65 lines
1.3 KiB
Python
Executable file
65 lines
1.3 KiB
Python
Executable file
#!/usr/bin/python
|
|
|
|
#
|
|
# Requirements:
|
|
#
|
|
# * salt
|
|
#
|
|
|
|
import salt.client
|
|
import salt.runner
|
|
import salt.config
|
|
from jinja2 import Template, BaseLoader, TemplateNotFound, FileSystemLoader, Environment
|
|
|
|
# settings
|
|
|
|
config = {
|
|
'base_path': 'www/',
|
|
'template_path': 'templates/',
|
|
}
|
|
|
|
# class definitions
|
|
|
|
class Inventorizer:
|
|
config = None
|
|
|
|
saltcmd = None
|
|
saltrun = None
|
|
|
|
host_list = {}
|
|
|
|
def __init__(self, config):
|
|
self.config = config
|
|
|
|
self.saltcmd = salt.client.LocalClient()
|
|
|
|
opts = salt.config.master_config('/etc/salt/master')
|
|
self.saltrun = salt.runner.RunnerClient(opts)
|
|
|
|
self.process()
|
|
|
|
def process(self):
|
|
self.collectHostData()
|
|
self.createHostList()
|
|
|
|
def collectHostData(self):
|
|
self.host_list = self.saltcmd.cmd('*', 'grains.items')
|
|
self.stat_list = self.saltrun.cmd('manage.status')
|
|
|
|
def createHostList(self):
|
|
fo = open(self.config['base_path'] + "hostlist.html", "wb")
|
|
env = Environment(loader = FileSystemLoader(config['template_path']))
|
|
template = env.get_template('hostlist_template.html')
|
|
fo.write(template.render({'hostlist': self.host_list, 'statlist': self.stat_list}))
|
|
fo.close
|
|
|
|
def lenFilter(self, list):
|
|
return len(list)
|
|
|
|
def replaceFilter(self, str, old, new):
|
|
return str(str).replace(old, new)
|
|
|
|
# main program
|
|
|
|
inv = Inventorizer(config)
|
|
|