#!/usr/bin/env python
from time import time
import subprocess


def cpu_metrics():
    metrics = {}

    now = int(time())
    p = subprocess.Popen(['/usr/bin/mpstat', '1', '1'], stdout=subprocess.PIPE)
    stdout, stderr = p.communicate()
    stdout = stdout.split('\n')[-2]
    stdout = [x for x in stdout.split(' ')[1:] if x]
    stdout = [int(float(x)) for x in stdout[1:]]
    for i, name in enumerate(('user', 'nice', 'sys', 'iowait', 'irq', 'soft', 'steal', 'guest', 'idle')):
        metrics[name] = {
            'ts': now,
            'type': 'GAUGE',
            'value': stdout[i],
        }

    return metrics


def mem_metrics():
    metrics = {}

    p = subprocess.Popen(['/usr/bin/vmstat', '-s'], stdout=subprocess.PIPE)
    stdout, stderr = p.communicate()
    now = int(time())

    for line in stdout.split('\n'):
        line = line.strip('\r\n\t ')
        if line.find(' K ') == -1:
            continue
        count, label = line.split(' K ', 1)
        label = label.replace(' ', '_')
        metrics[label] = {
            'ts': now,
            'type': 'GAUGE',
            'value': int(count),
        }
    return metrics


def network_metrics():
    p = subprocess.Popen(['/sbin/ifconfig'], stdout=subprocess.PIPE)
    stdout, stderr = p.communicate()

    for lines in stdout.split('\n\n'):
        if not lines.startswith('eth'):
            continue
        for line in lines.split('\n'):
            line = [x for x in line.split(' ') if x]
    return {}


def run_check(callback):
    callback('cpu', cpu_metrics())
    callback('memory', mem_metrics())
    callback('network', network_metrics())

@@ -1179,111 +1179,123 @@
     
-p = subprocess.Popen(%5B'/sbin/ifconfig'%5D, stdout=subprocess.PIPE)%0A    stdout, stderr = p.communicate
+metrics = %7B%7D%0A    now = int(time())%0A    fd = file('/proc/net/dev', 'r')%0A    head1 = fd.next()%0A    head2 = fd.next
 ()%0A
-%0A
     
@@ -1306,27 +1306,83 @@
 line
-s in stdout
+ in fd:%0A        line = line.strip('%5Cn ')%0A        iface, line = line
 .split('
 %5Cn%5Cn
@@ -1381,15 +1381,14 @@
 it('
-%5Cn%5Cn'):
+:', 1)
 %0A   
@@ -1399,21 +1399,21 @@
  if not 
-lines
+iface
 .startsw
@@ -1457,24 +1457,31 @@
     
-for 
 line
+ = %5Bx for x
  in line
 s.sp
@@ -1480,17 +1480,16 @@
 line
-s
 .split('
 %5Cn')
@@ -1488,66 +1488,626 @@
 it('
-%5Cn'):%0A            line = %5Bx for x in 
+ ') if x%5D%0A        metrics%5Biface%5D = %7B%0A            'rx_bytes': %7B%0A                'ts': now,%0A                'type': 'COUNTER',%0A                'value': int(line%5B0%5D),%0A            %7D,%0A            'tx_bytes': %7B%0A                'ts': now,%0A                'type': 'COUNTER',%0A                'value': int(
 line
-.split(' ') if x%5D
+%5B8%5D),%0A            %7D,%0A            'rx_packets': %7B%0A                'ts': now,%0A                'type': 'COUNTER',%0A                'value': int(line%5B1%5D),%0A            %7D,%0A            'tx_packets': %7B%0A                'ts': now,%0A                'type': 'COUNTER',%0A                'value': int(line%5B9%5D),%0A            %7D,%0A        %7D
 %0A   
@@ -2114,18 +2114,23 @@
  return 
-%7B%7D
+metrics
 %0A%0A%0Adef r

Add network metrics