Created
September 21, 2008 19:28
-
-
Save NaPs/11893 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| # coding=utf8 | |
| import cairo | |
| import pycha.line | |
| import subprocess | |
| from helpers import * | |
| # Parameters : | |
| mountpoint = '/mnt/data' | |
| logfilename = '/var/stats/df_%s.log' % mountpoint.replace('/', '_') | |
| output_filename = '/srv/http/monitoring/df_%s.png' % mountpoint.replace('/', '_') | |
| samples = 120 | |
| divs = 30 | |
| step = 4 | |
| timeunit = 'j' | |
| # Data collecting : | |
| df_pipe = subprocess.Popen('/bin/df | grep %s' % mountpoint, | |
| shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
| df_out, df_err = df_pipe.communicate() | |
| device, total, used, free, percent, mountpoint = df_out.split() | |
| # Logging : | |
| logfile = open(logfilename, 'a') | |
| logfile.write('%s;%s\n' % (used, float(used)/(float(used)+float(free))*100 )) | |
| logfile.close() | |
| # Chart generation : | |
| data = [(i, float(x[1][:-1])) for i, x in enumerate([l.split(';') for l in tail(logfilename, nol=samples).split('\n')])] | |
| dataset = ((mountpoint, data),) | |
| surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 600, 300) | |
| options = dict() | |
| options['padding'] = dict(left=50, right=30, top=30, bottom=30) | |
| options['legend'] = dict(position=dict(top=20, left=60)) | |
| options['title'] = u'Pourcentage d\'utilisation de %s (total %s) sur 1 mois' % (mountpoint, unit(int(total)*1024)) | |
| options['shouldFill'] = True | |
| options['colorScheme'] = '#800080' | |
| options['xOriginIsZero'] = True | |
| options['yOriginIsZero'] = True | |
| options['barWidthFillFraction'] = 1 | |
| options['axis'] = dict(y=dict(label=u'%', rotate=40, range=[0.0, 100.0]), x=dict(ticks=[dict(v=samples-y, label='%s%s'%(y/step, timeunit)) for y in [x*samples/divs for x in reversed(range(divs+1))]])) | |
| options['legend'] = dict(hide=True) | |
| chart = pycha.line.LineChart(surface, options) | |
| chart.addDataset(dataset) | |
| chart.render() | |
| surface.write_to_png(output_filename) |
This file contains hidden or 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 | |
| # coding=utf8 | |
| import cairo | |
| import pycha.line | |
| import subprocess | |
| from helpers import * | |
| # Parameters : | |
| disks = ['/dev/sda', '/dev/sdb', '/dev/sdc', '/dev/sdd', '/dev/sde', '/dev/sdf', '/dev/sdg'] | |
| logfilename = '/var/stats/hddtemp.log' | |
| output_filename = '/srv/http/monitoring/hddtemp.png' | |
| samples = 288 | |
| divs = 24 | |
| step = 12 | |
| timeunit = 'h' | |
| # Data collecting : | |
| temps = [] | |
| for disk in disks: | |
| hddtemp_pipe = subprocess.Popen('/usr/sbin/hddtemp -n -w %s' % disk, | |
| shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
| temp, err = hddtemp_pipe.communicate() | |
| temp = temp.strip() | |
| if not temp.isdigit(): | |
| temp = '-1' | |
| temps.append((disk, temp)) | |
| # Logging : | |
| logfile = open(logfilename, 'a') | |
| logfile.write(';'.join([':'.join(x) for x in temps])+'\n') | |
| logfile.close() | |
| # Chart generation : | |
| data = [[t.split(':') for t in l.split(';')] for l in tail(logfilename, nol=samples).split('\n')] | |
| # data -> [(('name', 'temp'), ('name', 'temp')), another line...] | |
| data_by_disk = dict() | |
| for disk in disks: | |
| data_by_disk[disk] = [] | |
| for i, line in enumerate(data): | |
| for disk, temp in line: | |
| if disk in data_by_disk and temp.isdigit(): | |
| data_by_disk[disk].append((i, float(temp))) | |
| dataset = [] | |
| for disk in disks: | |
| data = data_by_disk[disk] | |
| dataset.append((disk, data)) | |
| surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 600, 300) | |
| options = dict() | |
| options['padding'] = dict(left=50, right=30, top=30, bottom=30) | |
| options['legend'] = dict(position=dict(top=20, left=60)) | |
| options['title'] = u'Température des disques durs sur 24h' | |
| options['shouldFill'] = False | |
| options['colorScheme'] = '#800080' | |
| options['xOriginIsZero'] = False | |
| options['yOriginIsZero'] = False | |
| options['barWidthFillFraction'] = 1 | |
| options['axis'] = dict(y=dict(label=u'°C', rotate=40), x=dict(ticks=[dict(v=samples-y, label='%s%s'%(y/step, timeunit)) for y in [x*samples/divs for x in reversed(range(divs+1))]])) | |
| chart = pycha.line.LineChart(surface, options) | |
| chart.addDataset(dataset) | |
| chart.render() | |
| surface.write_to_png(output_filename) |
This file contains hidden or 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
| def tail(filepath, nol=10, read_size=1024): | |
| """ | |
| This function returns the last line of a file. | |
| Args: | |
| filepath: path to file | |
| nol: number of lines to print | |
| read_size: data is read in chunks of this size (optional, default=1024) | |
| Raises: | |
| IOError if file cannot be processed. | |
| """ | |
| f = open(filepath, 'rU') # U is to open it with Universal newline support | |
| offset = read_size | |
| f.seek(0, 2) | |
| file_size = f.tell() | |
| while 1: | |
| if file_size < offset: | |
| offset = file_size | |
| f.seek(-1*offset, 2) | |
| read_str = f.read(offset) | |
| # Remove newline at the end | |
| if read_str[offset - 1] == '\n': | |
| read_str = read_str[:-1] | |
| lines = read_str.split('\n') | |
| if len(lines) >= nol: # Got nol lines | |
| return "\n".join(lines[-nol:]) | |
| if offset == file_size: # Reached the beginning | |
| return read_str | |
| offset += read_size | |
| f.close() | |
| def unit(value): | |
| units = ['o', 'Ko', 'Mo', 'Go', 'To'] | |
| for unit in units: | |
| if float(value) >= 1024.0: | |
| value = float(value)/1024.0 | |
| else: | |
| return '%0.1f%s' % (value, unit) |
This file contains hidden or 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 | |
| # coding=utf8 | |
| import cairo | |
| import pycha.line | |
| import subprocess | |
| from helpers import * | |
| # Parameters : | |
| logfilename = '/var/stats/loadavg.log' | |
| output_filename = '/srv/http/monitoring/loadavg.png' | |
| samples = 60 | |
| divs = 4 | |
| timeunit = 'm' | |
| # Data collecting : | |
| loadavg = open('/proc/loadavg', 'r').readline() | |
| loadavg = loadavg.split() | |
| # Logging : | |
| logfile = open(logfilename, 'a') | |
| logfile.write('%s;%s;%s\n' % (loadavg[0], loadavg[1], loadavg[2])) | |
| logfile.close() | |
| # Chart generation : | |
| datafile = tail(logfilename, nol=samples).split('\n') | |
| data = [(i, float(x[0])) for i, x in enumerate([l.split(';') for l in datafile])] | |
| rangemax = float(int(max([float(x.split(';')[0]) for x in datafile]))+1) | |
| dataset = (('Load average', data),) | |
| surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 600, 300) | |
| options = dict() | |
| options['padding'] = dict(left=50, right=30, top=30, bottom=30) | |
| options['legend'] = dict(position=dict(top=20, left=60)) | |
| options['title'] = u'Charge moyenne sur 1h' | |
| options['shouldFill'] = True | |
| options['colorScheme'] = '#800080' | |
| #options['xOriginIsZero'] = True | |
| #options['yOriginIsZero'] = True | |
| #options['barWidthFillFraction'] = 1 | |
| options['axis'] = dict(y=dict(label=u'load', rotate=40, range=[0.0, rangemax]), x=dict(hide=False, ticks=[dict(v=samples-y, label='%s%s'%(y, timeunit)) for y in [x*samples/divs for x in reversed(range(divs+1))]])) | |
| options['legend'] = dict(hide=True) | |
| chart = pycha.line.LineChart(surface, options) | |
| chart.addDataset(dataset) | |
| chart.render() | |
| surface.write_to_png(output_filename) |
This file contains hidden or 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 | |
| # coding=utf8 | |
| import cairo | |
| import pycha.line | |
| import subprocess | |
| from helpers import * | |
| # Parameters : | |
| logfilename = '/var/stats/mem.log' | |
| output_filename = '/srv/http/monitoring/mem.png' | |
| samples = 60 | |
| divs = 4 | |
| step = 1 | |
| timeunit = 'm' | |
| # Data collecting : | |
| meminfo = open('/proc/meminfo', 'r') | |
| memmap = dict() | |
| for memitem in meminfo: | |
| memitem = memitem.strip().split() | |
| memmap[memitem[0][:-1]] = int(memitem[1]) | |
| # Logging : | |
| logfile = open(logfilename, 'a') | |
| logfile.write('%s\n' % (memmap['MemTotal'] - memmap['MemFree'] - memmap['Buffers'] - memmap['Cached'])) | |
| logfile.close() | |
| # Chart generation : | |
| datafile = tail(logfilename, nol=samples).split('\n') | |
| data = [(i, float(l)/1024) for i, l in enumerate(datafile)] | |
| dataset = (('Memory used', data),) | |
| surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 600, 300) | |
| options = dict() | |
| options['padding'] = dict(left=50, right=30, top=30, bottom=30) | |
| options['legend'] = dict(position=dict(top=20, left=60)) | |
| options['title'] = u'Utilisation de la mémoire (total %s) sur 1h' % unit(memmap['MemTotal']*1024) | |
| options['shouldFill'] = True | |
| options['colorScheme'] = '#800080' | |
| #options['xOriginIsZero'] = True | |
| #options['yOriginIsZero'] = True | |
| #options['barWidthFillFraction'] = 1 | |
| options['axis'] = dict(y=dict(label=u'Mo', rotate=40, range=[0.0, memmap['MemTotal']/1024.0]), x=dict(ticks=[dict(v=samples-y, label='%s%s'%(y/step, timeunit)) for y in [x*samples/divs for x in reversed(range(divs+1))]])) | |
| options['legend'] = dict(hide=True) | |
| chart = pycha.line.LineChart(surface, options) | |
| chart.addDataset(dataset) | |
| chart.render() | |
| surface.write_to_png(output_filename) |
This file contains hidden or 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 | |
| # coding=utf8 | |
| import cairo | |
| import pycha.line | |
| import subprocess | |
| from helpers import * | |
| import sys | |
| # Parameters : | |
| interface = 'eth0' | |
| logfilename = '/var/stats/vnstat_%s.log' % interface | |
| output_filename = '/srv/http/monitoring/vnstat_%s.png' % interface | |
| exec_time = 50 | |
| samples = 60 | |
| divs = 4 | |
| step = 1 | |
| timeunit = 'm' | |
| # Data collecting : | |
| vnstat_pipe = subprocess.Popen('vnstat -i %s -tr %s' % (interface, exec_time), | |
| shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
| vnstat_out, vnstat_err = vnstat_pipe.communicate() | |
| data = vnstat_out.split('\n') | |
| rx = float(data[3].split()[1]) | |
| rx_pkt = int(data[3].split()[3]) | |
| tx = float(data[4].split()[1]) | |
| tx_pkt = int(data[4].split()[3]) | |
| # Logging : | |
| logfile = open(logfilename, 'a') | |
| logfile.write('%s:%s:%s:%s\n' % (rx, rx_pkt, tx, tx_pkt)) | |
| logfile.close() | |
| # Chart generation : | |
| data = [l.split(':') for l in tail(logfilename, nol=samples).split('\n')] | |
| data_rx = [(i, float(x[0])) for i, x in enumerate(data)] | |
| data_tx = [(i, float(x[2])) for i, x in enumerate(data)] | |
| dataset = (('rx', data_rx), ('tx', data_tx)) | |
| surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 600, 300) | |
| options = dict() | |
| options['padding'] = dict(left=50, right=30, top=30, bottom=30) | |
| options['legend'] = dict(position=dict(top=20, left=60)) | |
| options['title'] = u'Débit réseau de %s sur 1h' % interface | |
| options['shouldFill'] = False | |
| options['colorScheme'] = '#800080' | |
| options['xOriginIsZero'] = True | |
| options['yOriginIsZero'] = True | |
| options['barWidthFillFraction'] = 1 | |
| options['axis'] = dict(y=dict(label=u'kB/s', rotate=40), x=dict(ticks=[dict(v=samples-y, label='%s%s'%(y/step, timeunit)) for y in [x*samples/divs for x in reversed(range(divs+1))]])) | |
| chart = pycha.line.LineChart(surface, options) | |
| chart.addDataset(dataset) | |
| chart.render() | |
| surface.write_to_png(output_filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment