Archived
Intial commit
This commit is contained in:
commit
99def63a4f
3 files changed
+78
No files matched your search
@@ -0,0 +1,3 @@
|
||||
*.swp
|
||||
*.pyc
|
||||
local_settings.py
|
||||
@@ -0,0 +1,9 @@
|
||||
|
||||
LOGSTASH_URL = 'http://es.example.com:8080'
|
||||
LOGSTASH_USER = 'nutuser'
|
||||
LOGSTASH_PASSWORD = ''
|
||||
|
||||
try:
|
||||
from local_settings import *
|
||||
except ImportError:
|
||||
pass
|
||||
Executable
+66
@@ -0,0 +1,66 @@
|
||||
#!/usr/bin/env python
|
||||
# Nikolai Kristiansen <nikolaik@gmail.com>
|
||||
import json
|
||||
import sys
|
||||
import subprocess
|
||||
from collections import OrderedDict
|
||||
import datetime
|
||||
import requests
|
||||
|
||||
import settings
|
||||
|
||||
def to_python(status):
|
||||
integer_fields = [
|
||||
"driver.parameter.pollinterval",
|
||||
"ups.delay.shutdown",
|
||||
"ups.delay.start",
|
||||
"ups.load"
|
||||
]
|
||||
float_fields = [
|
||||
"battery.voltage",
|
||||
"battery.voltage.high",
|
||||
"battery.voltage.low",
|
||||
"input.frequency",
|
||||
"input.voltage",
|
||||
"input.voltage.fault",
|
||||
"output.voltage",
|
||||
"ups.temperature"
|
||||
]
|
||||
for k,v in status.items():
|
||||
if k in integer_fields:
|
||||
v = int(v)
|
||||
elif k in float_fields:
|
||||
v = float(v)
|
||||
|
||||
status[k] = v
|
||||
|
||||
return status
|
||||
|
||||
def get_status(psu_label):
|
||||
cmd = ['upsc', psu_label]
|
||||
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
stdout,stderr = p.communicate()
|
||||
|
||||
lines = stdout.strip().split('\n')
|
||||
kvs = [l.split(': ') for l in lines]
|
||||
status = to_python(OrderedDict(kvs))
|
||||
|
||||
return status
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
if len(sys.argv) != 2:
|
||||
print("Invalid num args.")
|
||||
print("Usage: argv[0] psu_label")
|
||||
exit(1)
|
||||
|
||||
output = {
|
||||
'poll_date': datetime.datetime.utcnow().isoformat(),
|
||||
'status': get_status(sys.argv[1])
|
||||
}
|
||||
json_output = json.dumps(output, indent=4)
|
||||
r = requests.post(
|
||||
settings.LOGSTASH_URL,
|
||||
data=json_output, headers={'content-type':'application/json'},
|
||||
auth=(settings.LOGSTASH_USER, settings.LOGSTASH_PASSWORD)
|
||||
)
|
||||
Reference in new issue
Block a user