This commit is contained in:
dave 2017-05-15 17:29:20 -07:00
parent ec819b2cad
commit aafe393b62
8 changed files with 143 additions and 119 deletions

View File

@ -1,11 +1,13 @@
from subprocess import Popen, PIPE
def ipaddr():
"""
Return first entry of hostname --all-ip-addresses
"""
return Popen(["hostname", "--all-ip-addresses"], stdout=PIPE).communicate()[0].decode().split(" ")[0].strip()
def hostname():
"""
Return system hostname from hostname -f

View File

@ -11,6 +11,7 @@ import json
import sys
import os
class MonitorDaemon(Thread):
def __init__(self, config):
Thread.__init__(self)
@ -202,21 +203,24 @@ class MonitorThread(Thread):
self.logger.debug("cancelling scheduler")
self.alive = False
def run_cli():
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-c", "--config", action="store", type="string", dest="config", help="Path to config file")
parser.add_option("-l", "--logging", action="store", dest="logging", help="Logging level", default="INFO", choices=['WARN', 'CRITICAL', 'WARNING', 'INFO', 'ERROR', 'DEBUG'])
parser.add_option("-l", "--logging", action="store", dest="logging", help="Logging level", default="INFO",
choices=['WARN', 'CRITICAL', 'WARNING', 'INFO', 'ERROR', 'DEBUG'])
(options, args) = parser.parse_args()
logging.basicConfig(level=getattr(logging, options.logging), format="%(asctime)-15s %(levelname)-8s %(name)s@%(filename)s:%(lineno)d %(message)s")
logging.basicConfig(level=getattr(logging, options.logging),
format="%(asctime)-15s %(levelname)-8s %(name)s@%(filename)s:%(lineno)d %(message)s")
logger = logging.getLogger("init")
logger.debug("options: %s" % options)
if options.config == None:
if options.config is None:
parser.print_help()
sys.exit()
@ -239,5 +243,6 @@ def run_cli():
print("")
daemon.shutdown()
if __name__ == '__main__':
run_cli()

View File

@ -1,5 +1,6 @@
from psutil import disk_io_counters
def diskio(disks=[]):
with open("/proc/uptime", "r") as f:
uptime = int(float(f.read().split(" ")[0]))
@ -25,6 +26,7 @@ def diskio(disks=[]):
}
yield(stats)
mapping = {
"diskio": {
"properties": {
@ -72,4 +74,3 @@ mapping = {
if __name__ == '__main__':
for item in diskio():
print(item)

View File

@ -1,5 +1,6 @@
from os import statvfs
def diskspace(filesystems=[]):
for fs in filesystems:
stats = statvfs(fs)
@ -23,6 +24,7 @@ def diskspace(filesystems=[]):
yield info
mapping = {
"diskspace": {
"properties": {
@ -67,6 +69,7 @@ mapping = {
}
}
if __name__ == '__main__':
for item in diskspace(filesystems=["/", "/dev"]):
print(item)

View File

@ -7,6 +7,7 @@ def load():
"load_15m": m15
}
mapping = {
"load": {
"properties": {
@ -23,6 +24,7 @@ mapping = {
}
}
if __name__ == '__main__':
for item in load():
print(item)

View File

@ -5,12 +5,17 @@ memline_pattern = re.compile(r'^(?P<key>[^\\:]+)\:\s+(?P<value>[0-9]+)(\s(?P<uni
computed_fields = {
"mempctused": lambda items: round((items["memtotal"] - items["memfree"]) / items["memtotal"], 2),
"mempctfree": lambda items: 1 - round((items["memtotal"] - items["memfree"]) / items["memtotal"], 2),
"mempctused_nocache": lambda items: round((items["memtotal"]-items["memfree"]-items["cached"])/items["memtotal"], 2),
"mempctfree_nocache": lambda items: 1-round((items["memtotal"]-items["memfree"]-items["cached"])/items["memtotal"], 2),
"swappctused": lambda items: round((items["swaptotal"]-items["swapfree"])/items["swaptotal"] if items["swaptotal"] > 0 else 0, 2),
"swappctfree": lambda items: 1-round((items["swaptotal"]-items["swapfree"])/items["swaptotal"] if items["swaptotal"] > 0 else 0, 2)
"mempctused_nocache": lambda items: round((items["memtotal"] - items["memfree"] - items["cached"]) /
items["memtotal"], 2),
"mempctfree_nocache": lambda items: 1 - round((items["memtotal"] - items["memfree"] - items["cached"]) /
items["memtotal"], 2),
"swappctused": lambda items: round((items["swaptotal"] - items["swapfree"]) /
items["swaptotal"] if items["swaptotal"] > 0 else 0, 2),
"swappctfree": lambda items: 1 - round((items["swaptotal"] - items["swapfree"]) /
items["swaptotal"] if items["swaptotal"] > 0 else 0, 2)
}
def meminfo(whitelist=[]):
if not whitelist:
whitelist = ["swaptotal", "swapfree", "swapcached",
@ -40,6 +45,7 @@ def meminfo(whitelist=[]):
yield result
mapping = {
"meminfo": {
"properties": {
@ -62,6 +68,7 @@ mapping = {
}
}
if __name__ == '__main__':
for item in meminfo():
print(item)

View File

@ -1,11 +1,12 @@
from glob import glob
import re
KTHREADD_PID = 2
PAT_REMOVE_PROC_SPACES = re.compile(r'(\([^\)]+\))')
def procs():
def procs():
# Get uid->name mapping
users = {}
with open('/etc/passwd', 'r') as passwd:
@ -13,7 +14,7 @@ def procs():
line = passwd.readline()
if not line:
break
uname,x,uid,gid,opts,home,shell = line.split(":")
uname, _, uid, gid, opts, home, shell = line.split(":")
users[int(uid)] = uname
# Get gid->groupname mapping
@ -23,7 +24,7 @@ def procs():
line = group.readline()
if not line:
break
gname,x,gid,y = line.split(":")
gname, _, gid, y = line.split(":")
groups[int(gid)] = gname
num_procs = 0
@ -39,7 +40,6 @@ def procs():
stat = PAT_REMOVE_PROC_SPACES.sub("PROCNAME", stat)
stat = stat.split(" ")
proc_id = int(stat[0])
proc_parent = int(stat[3])
if proc_parent == KTHREADD_PID:
@ -54,6 +54,7 @@ def procs():
yield {"procs": num_procs, "threads": num_threads, "kthreads": num_kthreads}
mapping = {
"procs": {
"properties": {
@ -70,6 +71,7 @@ mapping = {
}
}
if __name__ == '__main__':
for item in procs():
print(item)

View File

@ -2,6 +2,7 @@ def uptime():
with open("/proc/uptime", "r") as f:
yield {"uptime": int(float(f.read().split(" ")[0]))}
mapping = {
"uptime": {
"properties": {
@ -12,6 +13,7 @@ mapping = {
}
}
if __name__ == '__main__':
for item in uptime():
print(item)