I have a problem with inserting data into database…
The case is like this :
First i read a directory for a files, after that for each file
[LIST=1][*]
find a file which name started with cpu, parse the data into variables
find a file which name started with ram, parse the data into variables
find a file which name started with hdd, parse the data into variables
After all variables are collected, then i insert them into database.
Here is the script to do this:
[code=php]if($dir = opendir(‘D:/Received From Linux’)){
$files = array();
//mengumpulkan isi array
while(false !== ($thisFile = readdir($dir)))
{
$files[] = $thisFile;
}
closedir($dir);
//DB connection
include “dbcon.php”;
foreach($files as $file){
if(preg_match(“/cpu(D+)(d+)(.txt)/”,$file,$match)){//print_r($match);
copy(“D:/Received From Linux/”.$match[0].””,””.$match[0].””);
//getting server name
$serv_nm = “”.$match[1].””;
//getting device name
preg_match(“/cpu/”, $match[0], $device);
//getting the date
$day = substr($match[2],0,2);
$month = substr($match[2],2,2);
$year = substr($match[2],4,4);
include ‘desc_MOY.php’;
$cpu_tanggal = “$day $month $year”;
//getting the clock
$hour = substr($match[2],8,2);
$minutes = substr($match[2],10,2);
$cpu_time = “$hour:$minutes”;
$lines = file($match[0]); //membaca isi file
$usages = array();
$cpulines = array();
foreach($lines as $line){
if(preg_match(“/Cpu(s):/”,$line)){
$cpulines[] = $line;
foreach($cpulines as $cpuline){
if(preg_match_all(“/(d+).(d+) 45/” , $cpuline, $values)){
$cpu_us = “”.$values[0][0].””;
$cpu_us = str_replace(‘%’,”,$cpu_us);
$cpu_sy = “”.$values[0][1].””;
$cpu_sy = str_replace(‘%’,”,$cpu_sy);
$cpu_ni = “”.$values[0][2].””;
$cpu_ni = str_replace(‘%’,”,$cpu_ni);
$cpu_id = “”.$values[0][3].””;
$cpu_id = str_replace(‘%’,”,$cpu_id);
$cpu_wa = “”.$values[0][4].””;
$cpu_wa = str_replace(‘%’,”,$cpu_wa);
$cpu_hi = “”.$values[0][5].””;
$cpu_hi = str_replace(‘%’,”,$cpu_hi);
$cpu_si = “”.$values[0][6].””;
$cpu_si = str_replace(‘%’,”,$cpu_si);
$cpu_st = “”.$values[0][7].””;
$cpu_st = str_replace(‘%’,”,$cpu_st);
include “warn_color.php”;
if($cpu_usage_total < $green){
$warn = ‘#00CC00’;
$cpu_alert = “g”;
} else if($cpu_usage_total < $yellow){
$warn = ‘#FFFF00’;
$cpu_alert = “y”;
} else {
$warn = ‘#FF0000’;
$cpu_alert = “r”;
}
//Display Tes Purpose —————————————————————————————————–
$cpu_usage_total = ($cpu_us + $cpu_sy + $cpu_ni + $cpu_wa + $cpu_hi + $cpu_si + $cpu_st);
echo “<strong>Untuk file : $file <br>Server : $serv_nm <br>Tanggal : $cpu_tanggal<br>Waktu : $cpu_time</strong>”;
echo “<br><br>Sample Layout :”;
echo “<br><ul><li>CPU us = $cpu_us%</li>”;
echo “<li>CPU sy = $cpu_sy%</li>”;
echo “<li>CPU ni = $cpu_ni%</li>”;
echo “<li>CPU id = $cpu_id%</li>”;
echo “<li>CPU wa = $cpu_wa%</li>”;
echo “<li>CPU hi = $cpu_hi%</li>”;
echo “<li>CPU si = $cpu_si%</li>”;
echo “<li>CPU st = $cpu_st%</li></ul>”;
echo “<h3><font color=$warn>CPU usage total : $cpu_usage_total %</font></h3>”;
//—————————————————————————————————————–
}
}
}
}
echo “<hr>”;
$sub_total = 0;
unlink($file);
//Upload Data Into DB ———————————————————————————————————————————-
$cpu_upload = mysql_query(“INSERT INTO mntr_result(id_server,tgl_pantau,wkt_pantau,cpu_us,cpu_sy,cpu_ni,cpu_id,cpu_wa,cpu_hi,cpu_si,cpu_st,cpu_alert) VALUES(‘$serv_nm’,’$cpu_tanggal’,’$cpu_time’,’$cpu_us’,’$cpu_sy’,’$cpu_ni’,’$cpu_id’,’$cpu_wa’,’$cpu_hi’,’$cpu_si’,’$cpu_st’,’$cpu_alert’)”);
//————————————————————————————————————————————————
}
if(preg_match(“/hdd(D+)(d+)(.txt)/”, $file, $match)){ //print_r($match);
copy(“D:/Received From Linux/” . $match[0] . “”, “” . $match[0] . “”);
//getting device name
preg_match(“/hdd/”, $match[0], $device);
//getting server name
$serv_nm = “”.$match[1].””;
$txt = file_get_contents($match[0]);
//getting the date
$day = substr($match[2],0,2);
$month = substr($match[2],2,2);
$year = substr($match[2],4,4);
include ‘desc_MOY.php’;
$hdd_tanggal = “$day $month $year”;
//getting the clock
$hour = substr($match[2],8,2);
$minutes = substr($match[2],10,2);
$hdd_time = “$hour:$minutes”;
preg_match_all(“/b(d+)b/”, $txt, $var);
$hdd_total = (($var[0][0] + $var[0][4] + $var[0][8])*1024)/1024000000;
$hdd_total = substr($hdd_total,0,4);
$hdd_used = (($var[0][1] + $var[0][5] + $var[0][9])*1024)/1024000000;
$hdd_used = substr($hdd_used,0,4);
$hdd_free = $hdd_total – $hdd_used;
$hddp_used = ($hdd_used/$hdd_total)*100;
$hddp_free = ($hdd_free/$hdd_total)*100;
$hddp_total = ($hdd_total/$hdd_total)*100;
$hddp_used = str_replace(“,”,”.”,number_format($hddp_used));
$hddp_free = str_replace(“,”,”.”,number_format($hddp_free));
$hddp_total= str_replace(“,”,”.”,number_format($hddp_total));
if($hdd_used_ < 70){
$hdd_alert = “g”; //light green
}
elseif($used_ < 100){
$hdd_alert = “y”; //light yellow
} else {
$hdd_alert = “r”; //light red
}
//Display Tes Purpose ———————————————————————————————————————————-
echo “<strong>File : $file <br> Device : ” . $device[0] . “<br>Server : $serv_nm<br>Tanggal : $hdd_tanggal<br>Waktu : $hdd_time</strong>”;
echo “<ul><li>Total amount : $hdd_total GByte ($hddp_total %)</li><li>Used Amount : $hdd_used Byte ($hddp_used %)</li><li>Free Space : $hdd_free Byte ($hddp_free %)</li></ul><hr>”;
//Upload Data Into DB ——————————————–
$hdd_upload = mysql_query(“INSERT INTO mntr_result(hdd_used,hdd_free,hdd_total,hddp_used,hddp_free,hddp_total,hdd_alert) VALUES(‘$hdd_used’,’$hdd_free’,’$hdd_total’,’$hddp_used’,’$hddp_free’,’$hddp_total’,’$hdd_alert’)”);
//—————————————————————-
unlink($file);
}
if(preg_match(“/ram(D+)(d+)(.txt)/”, $file, $match)){
copy(“D:/Received From Linux/” . $match[0] . “”, “” . $match[0] . “”);
//getting device name
preg_match(“/ram/”, $match[0], $device);
$txt = file_get_contents($match[0]);
//getting the date
$day = substr($match[2],0,2);
$month = substr($match[2],2,2);
$year = substr($match[2],4,4);
include ‘desc_MOY.php’;
$ram_tanggal = “$day $month $year”;
//getting the clock
$hour = substr($match[2],8,2);
$minutes = substr($match[2],10,2);
$ram_time = “$hour:$minutes”;
preg_match_all(“/b(d+)b/”, $txt, $var);
list($ram_total,$ram_used,$ram_free) = $var[1];
$ramp_used = ($ram_used / $ram_total) * 100;
$ramp_free = ($ram_free / $ram_total) * 100;
$ramp_total = ($ram_total / $ram_total) * 100;
$ramp_used = number_format($ramp_used);
$ramp_free = number_format($ramp_free);
$ramp_total = number_format($ramp_total);
if($ram_used < 70){
$ram_alert = “g”; //light green
} elseif($ram_used < 100){
$ram_alert = “y”; //light yellow
} else {
$ram_alert = “r”; //light red
}
//Display Tes Purpose ————————————————————————————————————————————
echo “<strong>File : $file <br>Device : ” . $device[0] . “<br>Server : $serv_nm<br>Tanggal : $ram_tanggal<br>Waktu : $ram_time</strong>”;
echo “<ul><li>Total amount : $ram_total bytes ($ramp_total %)</li><li>Used Amount : $ram_used bytes ($ramp_used %)</li><li>Free Space : $ram_free bytes ($ramp_free %)</li></ul><hr>”;
//——————————————————————————————————————————————————–
unlink($file);
//Upload Data Into DB ———————————————————————————————————-
$ram_upload = mysql_query(“INSERT INTO mntr_result(ram_used,ram_free,ram_total,ramp_used,ramp_free,ramp_total,ram_alert) VALUES(‘$ram_used’,’$ram_free’,’$ram_total’,’$ramp_used’,’$ramp_free’,’$ramp_total’,’$ram_alert’)”);
//————————————————————————————————————————
}
}
}
This script is reading all file which name started with ‘cpu’, after that ‘hdd’ and ‘ram’.
But it inserting data into database like this pattern:
cpu
cpu
cpu
—hdd
—hdd
—hdd
——ram
——ram
——ram
What i want is like this pattern :
cpu hdd ram
cpu hdd ram
The point is, how can i append data into existing row in the database?