Skip to content

Instantly share code, notes, and snippets.

@chuangbo
Created April 6, 2016 04:20
Show Gist options
  • Save chuangbo/cbfc9121997b8bc2e4217b928d1d604f to your computer and use it in GitHub Desktop.
Save chuangbo/cbfc9121997b8bc2e4217b928d1d604f to your computer and use it in GitHub Desktop.
Bitbar plugin for netdata
#!/bin/env python
import requests
url = 'http://YOUR_HOST_NAME/api/v1/data'
def netdata(chart):
r = requests.get(url, params={
'chart': chart,
'points': 1,
'format': 'array',
'after': -2,
'options': 'jsonwrap',
})
return r.json()
def system_cpu():
data = netdata('system.cpu')
cpu = data['result'][0]
# convert to 2 dight
formated = "%0.2f%%" % cpu
if cpu > 30:
print "%s | color=red" % formated
elif cpu > 20:
print "%s | color=orange" % formated
else:
print formated
def system_load():
data = netdata('system.load')
print '%s %s %s' % tuple(data['latest_values'])
def apps_cpu():
data = netdata('apps.cpu')
tuple_data = zip(data['dimension_names'], data['latest_values'])
tuple_data.sort(key=lambda x: x[1], reverse=True)
print '\n'.join('%s %0.2f%%' % n for n in tuple_data if n[1])
if __name__ == '__main__':
system_cpu()
print '---'
system_load()
print '---'
apps_cpu()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment