Skip to content

Instantly share code, notes, and snippets.

@mochipon
Created March 25, 2017 08:08
Show Gist options
  • Save mochipon/83708f7fb8e2208f5733ba1ff734a8cf to your computer and use it in GitHub Desktop.
Save mochipon/83708f7fb8e2208f5733ba1ff734a8cf to your computer and use it in GitHub Desktop.
Monitor CPU temperature in Mackerel (using kernel driver coretemp)
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
from datetime import datetime
import glob
import json
import os
import re
import sys
import time
def GetTemp():
dict = {}
for path_label in glob.glob('/sys/devices/platform/coretemp.0/hwmon/hwmon0/temp*_label'):
f_label = open(path_label)
label_raw = f_label.read()
label = re.compile(r'\W+', re.UNICODE).sub('', label_raw)
f_label.close()
path_input = path_label.replace('_label', '_input')
f_input = open(path_input)
input = int(f_input.read())
f_input.close()
dict[label] = input
return dict
def DefGraph():
metadata = {
'graphs': {
'temp.cpu': {
'label': 'CPU Core Temperature',
'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 = GetTemp()
for label, input in sorted(data.items()):
print("temp.cpu.%s\t%.2f\t%d" % (label, input / 1000, time))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment