This repository has been archived on 2025-02-01. You can view files and clone it, but cannot push or open issues or pull requests.
salt-inventory/saltinventory.py

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.host_list = self.saltcmd.cmd('*', 'grains.items')
self.stat_list = self.saltrun.cmd('manage.status')
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({'hostlist': self.host_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)