Created
May 27, 2016 20:39
-
-
Save jbarratt/d93f580ba53848f4183b9e1d66ce2019 to your computer and use it in GitHub Desktop.
Gather stats from the softnas API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import requests | |
s = requests.Session() | |
def url_for(path): | |
return "https://localhost/softnas/" + path | |
def login(): | |
instance_id = requests.get( | |
"http://169.254.169.254/latest/meta-data/instance-id").text.strip() | |
login_url = url_for("login.php") | |
s.post( | |
login_url, | |
data={ | |
'username': 'softnas', | |
'password': instance_id}, | |
verify=False) | |
def query(opcode, **kwargs): | |
api_url = url_for("snserver/snserv.php") | |
data = {'opcode': opcode} | |
data.update(kwargs) | |
r = s.post(api_url, data=data, verify=False) | |
try: | |
return r.json()['records'] | |
except: | |
print "Error: " + r.text | |
return {} | |
def save_metrics(metrics): | |
file_path = "/var/lib/node_exporter/softnas_metrics.prom" | |
with open(file_path, "w") as of: | |
for m in metrics: | |
if 'help' in m: | |
of.write("# HELP {0} {1}\n".format(m['name'], m['help'])) | |
if 'type' in m: | |
of.write("# TYPE {0} {1}\n".format(m['name'], m['type'])) | |
if 'value' in m: | |
of.write("{0} {1}\n".format(m['name'], m['value'])) | |
def volume_metrics(vol_name): | |
""" Return a list of metrics for the target volume """ | |
vol_data = query('volumes') | |
rv = [] | |
for vol in vol_data: | |
if vol['vol_name'] != vol_name: | |
continue | |
for m in ('total', 'used', 'free'): | |
rv.append({'name': 'volume_' + m, | |
'type': 'gauge', | |
'value': vol[m + '_numeric']}) | |
return rv | |
def perfmon_metrics(): | |
""" Return a list of metrics from the perfmon dataset """ | |
perf_data = query('perfmon')[-1] | |
rv = [] | |
for m, val in perf_data.iteritems(): | |
if m.startswith('iops') or m.startswith('latency') or m == 'time': | |
continue | |
rv.append({'name': 'softnas_' + m, | |
'type': 'gauge', | |
'value': val}) | |
return rv | |
def snaprep_metrics(): | |
""" Return a list of metrics from the perfmon dataset """ | |
snaprep = query('snaprepcommand', command="snapreplicatestatus") | |
rv = [] | |
rv.append({'name': 'softnas_ha_active', | |
'type': 'gauge', | |
'help': 'Is HA Running? 0 == no, 1 == yes', | |
'value': snaprep['active']}) | |
return rv | |
def main(): | |
login() | |
metrics = volume_metrics("volume1") + perfmon_metrics() + snaprep_metrics() | |
save_metrics(metrics) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment