Skip to content

Instantly share code, notes, and snippets.

@miticojo
Created June 23, 2016 10:49
Show Gist options
  • Save miticojo/d0b79544298449ddcb1924e69c518fe4 to your computer and use it in GitHub Desktop.
Save miticojo/d0b79544298449ddcb1924e69c518fe4 to your computer and use it in GitHub Desktop.
Send to influxdb cpu and memory metrics
import argparse
from influxdb import InfluxDBClient
import time
import random
import psutil
import socket
USER = 'root'
PASSWORD = 'root'
DBNAME = 'tutorial'
def main(host='localhost', port=8086):
hostname = socket.gethostname()
cpu_series = [{
'name': "%s.cpu" % hostname,
'columns': ["time", "user", "nice", "system", "idle", "iowait", "irq", "softirq", "steal", "guest", "guest_nice"],
'points': []
}]
memory_series = [{
'name': "%s.memory" % hostname,
'columns': ["time", "total", "available", "percent", "used", "free", "active", "inactive", "buffers", "cached"],
'points': []
}]
client = InfluxDBClient(host, port, USER, PASSWORD, DBNAME)
while True:
val = psutil.cpu_times_percent()
pointValues = [int(time.time()),val.user, val.nice, val.system, val.idle, val.iowait, val.irq, val.softirq, val.steal, val.guest, val.nice]
cpu_series[0]['points'].append(pointValues)
client.write_points(cpu_series)
val = psutil.virtual_memory()
pointValues = [int(time.time()), val.total, val.available, val.percent, val.used, val.free, val.active, val.inactive, val.buffers, val.cached]
memory_series[0]['points'].append(pointValues)
client.write_points(memory_series)
time.sleep(60)
def parse_args():
parser = argparse.ArgumentParser(
description='example code to play with InfluxDB')
parser.add_argument('--host', type=str, required=False, default='localhost',
help='hostname influxdb http API')
parser.add_argument('--port', type=int, required=False, default=8086,
help='port influxdb http API')
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
main(host=args.host, port=args.port)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment