Skip to content

Instantly share code, notes, and snippets.

@mochipon
Last active June 11, 2017 05:35
Show Gist options
  • Save mochipon/683d7787fdf878e7c7a2ae51924dff25 to your computer and use it in GitHub Desktop.
Save mochipon/683d7787fdf878e7c7a2ae51924dff25 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
from datetime import datetime
import json
import os
import time
# https://github.com/nicmcd/vcgencmd
# pip3 install git+https://github.com/nicmcd/vcgencmd.git
import vcgencmd
def GetFrequency():
dict = {}
frequency_sources = vcgencmd.frequency_sources()
for frequency_source in frequency_sources:
dict[frequency_source] = vcgencmd.measure_clock(frequency_source) * 0.000001
return dict
def GetVoltage():
dict = {}
voltage_sources = vcgencmd.voltage_sources()
for voltage_source in voltage_sources:
dict[voltage_source] = vcgencmd.measure_volts(voltage_source)
return dict
def GetTemp():
dict = {}
dict['core'] = vcgencmd.measure_temp()
return dict
def DefGraph():
metadata = {
'graphs': {
'raspi.frequency': {
'label': 'Raspberry Pi - Clock Frequency',
'unit': 'float',
'metrics': [{
'name': '*',
'label': '%1',
'stacked': False
}]
},
'raspi.voltage': {
'label': 'Raspberry Pi - Voltage',
'unit': 'float',
'metrics': [{
'name': '*',
'label': '%1',
'stacked': False
}]
},
'raspi.temp': {
'label': 'Raspberry Pi - Temperature of BCM2835 SoC',
'unit': 'float',
'metrics': [{
'name': '*',
'label': '%1',
'stacked': False
}]
}
}
}
print("# mackerel-agent-plugin")
print(json.dumps(metadata))
if __name__ == "__main__":
if os.environ.get('MACKEREL_AGENT_PLUGIN_META', '') == '1':
DefGraph()
sys.exit(0)
time = int(time.mktime(datetime.now().timetuple()))
data = {
'frequency': GetFrequency(),
'voltage': GetVoltage(),
'temp': GetTemp()
}
for topic, inputs in data.items():
for label, input in sorted(inputs.items()):
print("raspi.%s.%s\t%.2f\t%d" % (topic, label, input, time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment