This fixes the occurences where servers disappear because they are down on the grains call and up on the status call as calls are not transactional.
72 lines
1.6 KiB
Python
Executable file
72 lines
1.6 KiB
Python
Executable file
#!/usr/bin/python
|
|
|
|
#
|
|
# Requirements:
|
|
#
|
|
# * salt
|
|
#
|
|
|
|
import salt.client
|
|
import salt.runner
|
|
import salt.config
|
|
import salt.wheel
|
|
import time
|
|
from jinja2 import Template, BaseLoader, TemplateNotFound, FileSystemLoader, Environment
|
|
|
|
# settings
|
|
|
|
config = {
|
|
'base_path': '/var/www/html/saltstack/',
|
|
'template_path': 'templates/',
|
|
}
|
|
|
|
# class definitions
|
|
|
|
class Inventorizer:
|
|
config = None
|
|
|
|
saltcmd = None
|
|
saltrun = None
|
|
saltwhe = 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.saltwhe = salt.wheel.WheelClient(opts)
|
|
|
|
self.process()
|
|
|
|
def process(self):
|
|
self.collectHostData()
|
|
self.createHostList()
|
|
|
|
def collectHostData(self):
|
|
self.keys_list = self.saltwhe.cmd('key.list_all', [], {}, {})
|
|
self.stat_list = self.saltrun.cmd('manage.status')
|
|
self.grains_list = self.saltcmd.cmd('*', 'grains.items')
|
|
self.jobs_list = self.saltrun.cmd('jobs.list_jobs')
|
|
|
|
def createHostList(self):
|
|
fo = open(self.config['base_path'] + "index.html", "wb")
|
|
env = Environment(loader = FileSystemLoader(config['template_path']))
|
|
template = env.get_template('hostlist_template.html')
|
|
fo.write(template.render({'grainlist': self.grains_list, 'statlist': self.stat_list, 'keylist': self.keys_list, 'joblist': self.jobs_list, 'now' : time.strftime('%Y-%m-%d %H:%M:%S')}))
|
|
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)
|
|
|