Created
April 15, 2009 18:19
-
-
Save joshrendek/95937 to your computer and use it in GitHub Desktop.
This file contains 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
Make this a file, executable, add this to cron: | |
*/5 * * * * root /path/to/script; | |
Requirements: sysstat package on CentOS (yum install sysstat) || or just the sar command | |
#!/bin/env php | |
<?php | |
$host = "http://servly.com/rrd/update"; | |
error_reporting(E_NONE); | |
echo "\n"; | |
$mon = array(); | |
sleep(5); // so it reads properly | |
/* CPU Usage */ | |
$cpu = `sar 1 | awk 'END {print \$NF}'`; | |
$mon[cpu][free] = $cpu; | |
$mon[cpu][used] = 100-$mon[cpu][free]; | |
/* Load */ | |
$load = `uptime | awk -F"load average: " '{print \$2}' | awk '{print \$1}' | awk -F"," '{print \$1}'`; | |
/* MEMORY USAGE */ | |
$memory = `free | grep /+`; | |
$mon[mem][free] = `echo "$memory" | awk '{print $4}'`; | |
$mon[mem][used] = `echo "$memory" | awk '{print $3}'`; | |
$memory = `sar -r 1 | tail -n 1`; | |
$mon[mem][buffers] = `echo "$memory" | awk '{print $5}'`; | |
$mon[mem][cached] = `echo "$memory" | awk '{print $6}'`; | |
$memory = `free -m | grep Swap`; | |
$mon[mem][swapfree] = `echo "$memory" | awk '{print $4}'`; | |
$mon[mem][swapused] = `echo "$memory" | awk '{print $3}'`; | |
/* PROCESSES RUNNING */ | |
$mon[processes] = `ps ax | grep -c ""`; | |
/* NETWORK STATS */ | |
//$network = `sar -n DEV 1 -o | grep eth0 | tail -n 1`; | |
#cat /proc/net/dev | grep eth0 | cut -d: -f2 | awk '{ print $1":"$9}' | |
$network = `cat /proc/net/dev | grep eth0 | cut -d: -f2 | awk '{ print $1":"$9}'`; | |
$_n = explode(':', $network); | |
$mon[net][tx] = $_n[1]; | |
$mon[net][rx] = $_n[0]; | |
#$mon[net][tx] = `echo "$network" | awk '{print $9}'`; | |
#$mon[net][rx] = `echo "$network" | awk '{print $1}'`; | |
print_r($mon); | |
// lets send it to the host! | |
$post_data = array(); | |
$post_data['cpufree'] = | |
$mon[cpu][free]; | |
$post_data['freemem'] = | |
$mon[mem][free]; | |
$post_data['load'] = $load; | |
$post_data['mem_free'] = | |
$mon[mem][free]; | |
$post_data['mem_used'] = | |
$mon[mem][used]; | |
$post_data['mem_buffers'] = | |
$mon[mem][buffers]; | |
$post_data['mem_cached'] = | |
$mon[mem][cached]; | |
$post_data['net_tx'] = | |
$mon[net][tx]; | |
$post_data['net_rx'] = | |
$mon[net][rx]; | |
$post_data['procs'] = | |
$mon[processes]; | |
$post_data['swap_used'] = $mon[mem][swapused]; | |
$post_data['disk_size'] = $mon[disk][size]; | |
$post_data['disk_used']= $mon[disk][used]; | |
$url = $host; | |
$o=""; | |
foreach ($post_data as | |
$k=>$v) | |
{ | |
$o.= | |
"$k=".utf8_encode($v)."&"; | |
} | |
$post_data=substr($o,0,-1); | |
$ch = curl_init(); | |
curl_setopt($ch, | |
CURLOPT_POST, 1); | |
curl_setopt($ch, | |
CURLOPT_HEADER, 0); | |
curl_setopt($ch, CURLOPT_URL, | |
$url); | |
curl_setopt($ch, | |
CURLOPT_POSTFIELDS, $post_data); | |
$result = curl_exec($ch); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment