Doel
Beginner 2.0
kebetulan sedang ada waktu luang, iseng bikin script sederhana.
maap, kalau code-nya sedikit berantakan (atau tidak standar). harap maklum, lagi belajar
kritik atau saran akan diterima dengan senang hati. semoga bermanfaat.
prerequisites:
- package sysstat
1. membuat file crontab di VPS. misalnya seperti:
jalankan (schedule) script ini di crontab, misalnya setiap 10 menit sekali.
2. membuat script PHP untuk membaca (parsing) hasil output dari crontab. seperti:
3. membuat file PHP (mini-API) untuk menerima request dari luar (JSONP). seperti:
buka file "json.php" dari web (mis: http://website/apps/json.php). bila ok, akan tampil seperti ini:
selesai. mudah sekali kan? selanjutnya tinggal membuat aplikasi user-nya
tapi, sepertinya terlalu panjang kalau ditulis dimari. lagipula, terlalu banyak teori, tidak asik...
ini demo sederhana, untuk aplikasi webapps.
file:
- uptime.sh: http://mungil.or.id/pantau/src/uptime.txt
- tools.php : http://mungil.or.id/pantau/src/tools.txt
- json.php : http://mungil.or.id/pantau/src/json.txt
maap, kalau code-nya sedikit berantakan (atau tidak standar). harap maklum, lagi belajar

kritik atau saran akan diterima dengan senang hati. semoga bermanfaat.

prerequisites:
- package sysstat
1. membuat file crontab di VPS. misalnya seperti:
Code:
#!/bin/bash
# filename: uptime.sh
mydir=/direktori/untuk/menyimpan/data
f1="my-uptime.txt"
f2="my-memory.txt"
f3="my-storage.txt"
f4="my-iostat.txt"
uptime > $mydir/$f1
free -m > $mydir/$f2
df -h > $mydir/$f3
iostat > $mydir/$f4
jalankan (schedule) script ini di crontab, misalnya setiap 10 menit sekali.
Code:
*/10 * * * * /usr/local/bin/uptime.sh
2. membuat script PHP untuk membaca (parsing) hasil output dari crontab. seperti:
PHP:
<?php
// filename: tools.php
$data_dir = "/direktori/untuk/menyimpan/data";
// file uptime (disesuaikan dengan nama di crontab)
$file = $data_dir."/"."my-uptime.txt";
// file memory (disesuaikan dengan nama di crontab)
$file2 = $data_dir."/"."my-memory.txt";
// file storage (disesuaikan dengan nama di crontab)
$file3 = $data_dir."/"."my-storage.txt";
// file iostat (disesuaikan dengan nama di crontab)
$file4 = $data_dir."/"."my-iostat.txt";
function cleanup( $t ) {
$t = trim( $t );
$t = str_replace( array( '\r\n','\n','\r'), '', $t );
return $t;
}
function my_filter( $array ) {
$new_array = array();
foreach( $array as $k => $v ) {
if( $v =='' || $v =='Mem:' || $v =='-/+' || $v =='buffers/cache:' ) {
// nothing to do...
} else {
$new_array[] = cleanup( $v );
}
}
return $new_array;
}
function vps_uptime(){
global $file;
$tmp = file_get_contents( $file, NULL, NULL, 10);
$data = explode( ',', $tmp );
$a = str_replace( 'up', '', $data[0] );
trim( $a );
$up = explode( ' ', $a );
$uptime["hari"] = $up[1];
if( preg_match("/([0-9]{1,2}):([0-9]){1,2}/", $data[1] ) ) {
$up2 = explode( ':', $data[1] );
$uptime["jam"] = cleanup( $up2[0] );
$uptime["menit"] = cleanup( $up2[1] );
} else {
$uptime["jam"] = "";
$uptime["menit"] = "";
} return $uptime;
}
function vps_memory() {
global $file2;
$tmp = file( $file2 );
$memory = explode( ' ', $tmp[1] );
$mem = my_filter( $memory );
$a["memory"]["total"] = $mem[0];
$a["memory"]["used"] = $mem[1];
$a["memory"]["free"] = $mem[2];
$cache = explode( ' ', $tmp[2] );
$cac = my_filter( $cache );
$a["cache"]["used"] = $cac[0];
$a["cache"]["free"] = $cac[1];
return $a;
}
function vps_storage() {
global $file3;
$tmp = file( $file3 );
$storage = explode( ' ', $tmp[1] );
$sto = my_filter( $storage );
$s["total"] = $sto[1];
$s["used"] = $sto[2];
$s["free"] = $sto[3];
$s["percent"] = $sto[4];
return $s;
}
function vps_iostat() {
global $file4;
$tmp = file( $file4 );
$iotmpa = explode( ' ', $tmp[2] );
$iotmp = explode( ' ', $tmp[3] );
$a = my_filter( $iotmp );
$io["cpu"]["user"] = $a[0];
$io["cpu"]["nice"] = $a[1];
$io["cpu"]["system"] = $a[2];
$io["cpu"]["wait"] = $a[3];
$io["cpu"]["steal"] = $a[4];
$io["cpu"]["idle"] = $a[5];
return $io;
}
function collect_data() {
$data = array();
$data["uptime"] = vps_uptime();
$data["storage"] = vps_storage();
$data += vps_memory();
$data += vps_iostat();
return $data;
}
function show_uptime( $echo = true ) {
global $sysinfo;
$a["uptime"] = vps_uptime();
$uptime = $a["uptime"]["hari"] . " hari, ";
if( !empty( $a["uptime"]["jam"] ) )
$uptime .= $a["uptime"]["jam"] . " jam, ";
if( !empty( $a["uptime"]["menit"] ) )
$uptime .= $a["uptime"]["menit"] . " menit";
if( $echo )
echo $uptime;
else
return $uptime;
}
function show_memory( $what = "memtotal" ) {
$memory = vps_memory();
switch ( $what ) {
case "memtotal":
echo $memory["memory"]["total"];
break;
case "memused":
echo $memory["memory"]["used"];
break;
case "memfree":
echo $memory["memory"]["free"];
break;
case "cacused":
echo $memory["cache"]["used"];
break;
case "cacfree":
echo $memory["cache"]["free"];
break;
}
}
function show_storage( $what = 'used' ) {
global $sysinfo;
switch ( $what ) {
case "total":
echo $sysinfo["storage"]["total"];
break;
case "used":
echo $sysinfo["storage"]["used"];
break;
case "free":
echo $sysinfo["storage"]["free"];
break;
case "percent":
echo $sysinfo["storage"]["percent"];
break;
}
}
3. membuat file PHP (mini-API) untuk menerima request dari luar (JSONP). seperti:
PHP:
<?php
// filename: json.php
require_once "tools.php";
// sunting dimari
$vpsnode = "Nama VM";
header("content-type: application/json; charset=utf-8");
$data["node"][$vpsnode] = collect_data();
echo $_GET["callback"] . "(".json_encode($data).")";
?>
buka file "json.php" dari web (mis: http://website/apps/json.php). bila ok, akan tampil seperti ini:
Code:
[COLOR=#000000]({"node":{"[/COLOR]Nama VM[COLOR=#000000]":{"uptime":{"hari":"1","jam":"2","menit":"58"},"storage":{"total":"15G","used":"768M","free":"15G","percent":"5%"},"memory":{"total":"256","used":"87","free":"168"},"cache":{"used":"87","free":"168"},"cpu":{"user":"0.01","nice":"0.00","system":"0.00","wait":"0.08","steal":"0.00","idle":"99.91"}}}})[/COLOR]
selesai. mudah sekali kan? selanjutnya tinggal membuat aplikasi user-nya

tapi, sepertinya terlalu panjang kalau ditulis dimari. lagipula, terlalu banyak teori, tidak asik...

ini demo sederhana, untuk aplikasi webapps.
file:
- uptime.sh: http://mungil.or.id/pantau/src/uptime.txt
- tools.php : http://mungil.or.id/pantau/src/tools.txt
- json.php : http://mungil.or.id/pantau/src/json.txt
Last edited: