Ejemplo n.º 1
0
 /**
  * logDiskUsageData
  *
  * Retrives data and logs it to file
  *
  * @param string $type type of logging default set to normal but it can be API too.
  * @return string $string if type is API returns data as string
  *	 *
  */
 public function logData($type = false)
 {
     $class = __CLASS__;
     $settings = Logger::$_settings->{$class};
     $timestamp = time();
     //get process data here
     $ps_args = '-Ao %cpu,%mem,pid,user,comm,args';
     putenv('COLUMNS=1000');
     $string = shell_exec("ps {$ps_args}");
     //$string = explode("\n", trim ($processData));
     //get log location
     $logdirname = sprintf($this->logdir, date('Y-m-d'));
     $logpath = LOG_PATH . $logdirname;
     //echo "LOG_PATH : " . $logpath . " \n";
     $filename = sprintf($this->logfile, $logdirname, $timestamp);
     //echo "FILENAME : " . $filename . " \n";
     //great log folder if it doesnt exist
     if (!file_exists($logpath)) {
         mkdir($logpath, 0777);
     }
     //read me on encoding array to disk instead
     //https://www.safaribooksonline.com/library/view/php-cookbook/1565926811/ch05s08.html
     //write out process data to file
     LoadUtility::safefilerewrite($filename, $string, "w", true);
     if ($type == "api") {
         return $string;
     } else {
         return true;
     }
 }
Ejemplo n.º 2
0
 /**
  * logDiskUsageData
  *
  * Retrives data and logs it to file
  *
  * @param string $type type of logging default set to normal but it can be API too.
  * @return string $string if type is API returns data as string
  *	 *
  */
 public function logData($type = false)
 {
     $class = __CLASS__;
     $settings = Logger::$_settings->{$class};
     $timestamp = time();
     $drive = $settings['settings']['drive'];
     if (is_dir($drive)) {
         $spaceBytes = disk_total_space($drive);
         $freeBytes = disk_free_space($drive);
         $usedBytes = $spaceBytes - $freeBytes;
         //$freeBytes = dataSize($Bytes);
         //$percentBytes = $freeBytes ? round($freeBytes / $totalBytes, 2) * 100 : 0;
     }
     $string = $timestamp . '|' . $usedBytes . '|' . $spaceBytes . "\n";
     $filename = sprintf($this->logfile, date('Y-m-d'));
     LoadUtility::safefilerewrite($filename, $string, "a", true);
     //If alerts are enabled, check for alerts
     if (Alert::$alertStatus) {
         $this->checkAlerts($timestamp, $usedBytes, $spaceBytes, $settings);
     }
     if ($type == "api") {
         return $string;
     } else {
         return true;
     }
 }
Ejemplo n.º 3
0
 /**
  * logMemoryUsageData
  *
  * Retrives data and logs it to file
  *
  * @param string $type type of logging default set to normal but it can be API too.
  * @return string $string if type is API returns data as string
  *
  */
 public function logData($type = false)
 {
     $class = __CLASS__;
     $settings = Logger::$_settings->{$class};
     $timestamp = time();
     /* 
     	grab this data directly from /proc/meminfo in a single call
     	egrep --color 'Mem|Cache|Swap' /proc/meminfo
     */
     //pulling Cached here gives us both Cached and SwapCached
     exec("egrep 'SwapCached|SwapTotal|SwapFree' /proc/meminfo | awk -F' ' '{print \$2}'", $sysmemory);
     /*
       [0]=> string(11) "SwapCached:"
       [1]=> string(10) "SwapTotal:"
       [2]=> string(9) "SwapFree:"
     */
     //calculate swap usage
     $swapcached = $sysmemory[0];
     $swaptotal = $sysmemory[1];
     $swapfree = $sysmemory[2];
     $swapused = $swaptotal - ($swapfree + $swapcached);
     $string = $timestamp . '|' . $swapcached . '|' . $swaptotal . '|' . $swapfree . '|' . $swapused . "\n";
     //echo 'DATA:'  . $string .  "\n" ;
     $filename = sprintf($this->logfile, date('Y-m-d'));
     LoadUtility::safefilerewrite($filename, $string, "a", true);
     //If alerts are enabled, check for alerts
     if (Alert::$alertStatus) {
         $this->checkAlerts($timestamp, $swapused, $swaptotal, $settings);
     }
     if ($type == "api") {
         return $string;
     } else {
         return true;
     }
 }
Ejemplo n.º 4
0
 /**
  * logApacheUsageData
  *
  * Retrives data and logs it to file
  *
  * @param string $type type of logging default set to normal but it can be API too.
  * @return string $string if type is API returns data as string
  *
  */
 public function logData($type = false)
 {
     $class = __CLASS__;
     $settings = Logger::$_settings->{$class};
     //$url = "http://localhost/server-status";
     $url = $settings['settings']['serverstatus'];
     $parseUrl = $url . "/?auto";
     $locate = "CPULoad";
     //float $dataValue;
     $dataValue = $this->getApacheDataValue($parseUrl, $locate);
     //$dataValue = sscanf($dataValue, "%f")[0];
     //$dataValue = floatval($dataValue);
     //$dataValue = (float)$dataValue;
     //echo 'APACHE:' . $dataValue;
     //settype($dataValue, "float");
     if ($dataValue == null) {
         $dataValue = 0;
     }
     $string = time() . '|' . $dataValue . "\n";
     $filename = sprintf($this->logfile, date('Y-m-d'));
     //$this->safefilerewrite($filename,$string,"a",true);
     LoadUtility::safefilerewrite($filename, $string, "a", true);
     if ($type == "api") {
         return $string;
     } else {
         return true;
     }
 }
Ejemplo n.º 5
0
 /**
  * logData
  *
  * Retrives data and logs it to file
  *
  * @param string $type type of logging default set to normal but it can be API too.
  * @return string $string if type is API returns data as string
  *
  */
 public function logData($type = false)
 {
     $class = __CLASS__;
     $settings = Logger::$_settings->{$class};
     $timestamp = time();
     $load = null;
     //use the php function if its there
     if (!function_exists('sys_getloadavg')) {
         $load = exec("cat /proc/loadavg | awk -F' ' '{print \$1\"|\"\$2\"|\"\$3}'");
     } else {
         $phpload = sys_getloadavg();
         $load = $phpload[0] . "|" . $phpload[1] . "|" . $phpload[2];
     }
     //if we want fancy formatting in logs we can always format them like this
     //$number = number_format((float)$number, 2, '.', '');
     $string = $timestamp . '|' . $load . "\n";
     //we can also add a switch to feed live data to server with no local logging
     //by just returning data
     $filename = sprintf($this->logfile, date('Y-m-d'));
     LoadUtility::safefilerewrite($filename, $string, "a", true);
     //If alerts are enabled, check for alerts
     //note: $phpload dont work on 4.0 needs fixing above
     if (Alert::$alertStatus) {
         $alertString = $this->checkAlerts($timestamp, $phpload, $settings);
     }
     //Based on API mode return data if need be
     if ($type == "api") {
         return $string;
     } else {
         return true;
     }
 }
Ejemplo n.º 6
0
 /**
  * writeAlerts - writes alerts out to log file
  *
  */
 public function writeAlerts()
 {
     //hard coded for the moment
     if (isset(self::$alertDataArray) && is_array(self::$alertDataArray)) {
         $filename = LOG_PATH . "events_" . date('Y-m-d') . ".log";
         LoadUtility::safefilerewrite($filename, self::$alertDataArray, "a", true);
         return true;
     }
     return false;
 }
Ejemplo n.º 7
0
 /**
  * logData
  *
  * Retrives uptime data and logs it to file
  *
  * @param string $type type of logging default set to normal but it can be API too.
  * @return string $string if type is API returns data as string
  *	 *
  */
 public function logData($type = false)
 {
     $class = __CLASS__;
     $settings = Logger::$_settings->{$class};
     $uptime = exec("cat /proc/uptime | awk -F' ' '{print \$1\"|\"\$2}'");
     $string = time() . '|' . $uptime . "\n";
     $filename = sprintf($this->logfile, date('Y-m-d'));
     LoadUtility::safefilerewrite($filename, $string, "a", true);
     if ($type == "api") {
         return $string;
     } else {
         return true;
     }
 }
Ejemplo n.º 8
0
 /**
  * logMemoryUsageData
  *
  * Retrives data and logs it to file
  *
  * @param string $type type of logging default set to normal but it can be API too.
  * @return string $string if type is API returns data as string
  *
  */
 public function logData($type = false)
 {
     $class = __CLASS__;
     $settings = Logger::$_settings->{$class};
     $timestamp = time();
     /* 
     	grab this data directly from /proc/meminfo in a single call
     	egrep --color 'Mem|Cache|Swap' /proc/meminfo
     */
     //pulling Cached here gives us both Cached and SwapCached
     exec("egrep 'MemTotal|MemFree|Buffers|Cached|SwapTotal|SwapFree' /proc/meminfo | awk -F' ' '{print \$2}'", $sysmemory);
     /*
       [0]=> string(9) "MemTotal:"
       [1]=> string(8) "MemFree:"
       [2]=> string(8) "Buffers:"
       [3]=> string(7) "Cached:"
       [4]=> string(11) "SwapCached:"
       [5]=> string(10) "SwapTotal:"
       [6]=> string(9) "SwapFree:"
     */
     //calculate memory usage
     $memory = 0;
     $totalmemory = $sysmemory[0];
     $freememory = $sysmemory[1];
     $bufferedmemory = $sysmemory[2];
     $cachedmemory = $sysmemory[3];
     $memory = $totalmemory - ($freememory + $bufferedmemory + $cachedmemory);
     //calculate swap usage
     $swapcached = $sysmemory[4];
     $totalswap = $sysmemory[5];
     $freeswap = $sysmemory[6];
     $swap = $totalswap - ($freeswap + $swapcached);
     //pull log data togeather
     $string = $timestamp . '|' . $memory . '|' . $swap . '|' . $totalmemory . "\n";
     //append log data to log file
     $filename = sprintf($this->logfile, date('Y-m-d'));
     LoadUtility::safefilerewrite($filename, $string, "a", true);
     //If alerts are enabled, check for alerts
     if (Alert::$alertStatus) {
         $this->checkAlerts($timestamp, $memory, $totalmemory, $settings);
     }
     if ($type == "api") {
         return $string;
     } else {
         return true;
     }
 }
Ejemplo n.º 9
0
 /**
  * logData
  *
  * Retrives data and logs it to file
  *
  * @param string $type type of logging default set to normal but it can be API too.
  * @return string $string if type is API returns data as string
  *
  */
 public function logData($type = false)
 {
     $class = __CLASS__;
     $settings = Logger::$_settings->{$class};
     $timestamp = time();
     $r = null;
     //grab server location and port
     $server = $settings['settings']['server'];
     $port = intval($settings['settings']['port']);
     //echo 'server : ' . $server;
     //echo ' port : ' . $port;
     //grab data from API
     $r = $this->request('summary', $server, $port);
     /*
     echo print_r($r["SUMMARY"]["MHS 1m"], true)."\n";
     echo print_r($r["SUMMARY"]["MHS 5m"], true)."\n";
     echo print_r($r["SUMMARY"]["MHS 15m"], true)."\n";
     */
     $load = $r["SUMMARY"]["MHS 1m"] . "|" . $r["SUMMARY"]["MHS 5m"] . "|" . $r["SUMMARY"]["MHS 15m"];
     //if we want fancy formatting in logs we can always format them like this
     //$number = number_format((float)$number, 2, '.', '');
     $string = $timestamp . '|' . $load . "\n";
     //we can also add a switch to feed live data to server with no local logging
     //by just returning data
     $filename = sprintf($this->logfile, date('Y-m-d'));
     LoadUtility::safefilerewrite($filename, $string, "a", true);
     //If alerts are enabled, check for alerts
     //note: $phpload dont work on 4.0 needs fixing above
     if (Alert::$alertStatus) {
         $alertString = $this->checkAlerts($timestamp, $r, $settings);
     }
     //Based on API mode return data if need be
     if ($type == "api") {
         return $string;
     } else {
         return true;
     }
 }
Ejemplo n.º 10
0
 public function logData($type = false)
 {
     $class = __CLASS__;
     $settings = Logger::$_settings->{$class};
     $sshdLogFile['path'] = $settings['settings']['log_location'];
     //log data variables
     $logData['invalid_user'] = 0;
     $logData['failed_pass'] = 0;
     $logData['accepted'] = 0;
     //grab the logfile
     $logfile = sprintf($this->logfile, date('Y-m-d'));
     //check if log file exists and see time difference
     //stored in elapsed
     if ($logfile && file_exists($logfile)) {
         $elapsed = time() - filemtime($logfile);
     } else {
         $elapsed = 0;
     }
     //meaning new logfile
     //we need to read offset here
     //grab net latest location and figure out elapsed
     //zero out offset
     $sshdLogFile['offset'] = 0;
     $sshdLogFile['timestamp'] = 0;
     $sshlatestElapsed = 0;
     $sshLatestLocation = dirname($logfile) . DIRECTORY_SEPARATOR . '_ssh_latest';
     // basically if sshlatestElapsed is within reasonable limits (logger interval + 20%)
     // then its from the day before rollover so we can use it to replace regular elapsed
     // which is 0 when there is a new log file
     if (file_exists($sshLatestLocation)) {
         //if we want to add more data to return string we can use eplode below
         $last = explode("|", file_get_contents($sshLatestLocation));
         $sshdLogFile['offset'] = file_get_contents($sshLatestLocation);
         //$sshdLogFile['timestamp'] = file_get_contents(  $sshLatestLocation );
         //echo 'STORED OFFSET  : ' . $sshdLogFile['offset']   . "\n";
         $sshlatestElapsed = time() - filemtime($sshLatestLocation);
         //if its a new logfile check to see if whats up with the interval
         if ($elapsed == 0) {
             //data needs to within the logging period limits to be accurate
             $interval = $this->getLoggerInterval();
             if (!$interval) {
                 $interval = 360;
             } else {
                 $interval = $interval * 1.2;
             }
             if ($sshlatestElapsed <= $interval) {
                 $elapsed = $sshlatestElapsed;
             }
         }
     }
     // Reset offset if file size has reduced (truncated)
     // means logs have been rotated!
     // TODO :
     // if logs have been rotated we need to look for data in old log file
     // and add to new log file
     // however need to read a .gz to do this as old logs are compressed and soted by date
     // ie secure-20140427.gz
     $fileSize = filesize($sshdLogFile['path']);
     if ($fileSize < $sshdLogFile['offset']) {
         $sshdLogFile['offset'] = 0;
     }
     //read log file and get log data
     if (!$this->loadLogData($sshdLogFile, $logData)) {
         return false;
     }
     //if we were able to get last data from mysql latest above
     //figure out the difference as thats what we chart
     if (@$sshdLogFile['offset'] && $elapsed) {
         if ($logData['accepted'] < 0) {
             $logData['accepted'] = 0;
         }
         if ($logData['failed_pass'] < 0) {
             $logData['failed_pass'] = 0;
         }
         if ($logData['invalid_user'] < 0) {
             $logData['invalid_user'] = 0;
         }
         $string = time() . "|" . $logData['accepted'] . "|" . $logData['failed_pass'] . "|" . $logData['invalid_user'] . "\n";
         //echo 'DATA WRITE  : ' . $logData['accepted'] . '|' . $logData['failed_pass'] . '|' . $logData['invalid_user'] . "\n";
     } else {
         //if this is the first value in the set and there is no previous data then its null
         $lastlogdata = "|0|0|0";
         $string = time() . $lastlogdata . "\n";
     }
     //write out log data here
     LoadUtility::safefilerewrite($logfile, $string, "a", true);
     // write out filesize so we can pick up where we left off next time around
     LoadUtility::safefilerewrite($sshLatestLocation, $fileSize, "w", true);
     if ($type == "api") {
         return $string;
     } else {
         return true;
     }
 }
Ejemplo n.º 11
0
 /**
  * logData
  *
  * Retrives data and logs it to file
  *
  * @param string $type type of logging default set to normal but it can be API too.
  * @return string $string if type is API returns data as string
  *
  */
 public function logData($type = false)
 {
     $class = __CLASS__;
     $settings = Logger::$_settings->{$class};
     $timestamp = time();
     //need to collect data from multiple interfaces here
     $apiString = "";
     foreach (Logger::$_settings->general['network_interface'] as $interface => $value) {
         //echo 'NET: ' . $interface . "\n" ;
         //skip disabled interfaces - should be and / or not and ?
         if (!(isset(Logger::$_settings->general['network_interface'][$interface]) && Logger::$_settings->general['network_interface'][$interface] == "true")) {
             continue;
         }
         $logfile = sprintf($this->logfile, date('Y-m-d'), $interface);
         //echo 'PROCESSING:' . $logfile . "\n";
         $netdev = file_get_contents('/proc/net/dev');
         $pattern = "/^.*\\b({$interface})\\b.*\$/mi";
         preg_match($pattern, $netdev, $hits);
         $venet = '';
         if (isset($hits[0])) {
             $venet = trim($hits[0]);
             $venet = preg_replace("/ {1,99}/", " ", $venet);
             $venet = trim(str_replace("{$interface}:", "", $venet));
         }
         $parts = explode(" ", $venet);
         $recv = isset($parts[0]) ? $parts[0] : '';
         $trans = isset($parts[8]) ? $parts[8] : '';
         // $recv = exec("cat /proc/net/dev | grep ".$interface." | awk -F' ' '{print $2}'");
         // $trans = exec("cat /proc/net/dev | grep ".$interface." | awk -F' ' '{print $10}'");
         if ($logfile && file_exists($logfile)) {
             $elapsed = $timestamp - filemtime($logfile);
         } else {
             $elapsed = 0;
         }
         //meaning new logfile
         //used to help calculate the difference as network is thruput not value based
         //so is based on the difference between thruput before the current run
         //this data is stored in _net_latest_elapsed_
         // grab net latest location and elapsed
         $netlatestElapsed = 0;
         $netLatestLocation = dirname($logfile) . DIRECTORY_SEPARATOR . '_net_latest_' . $interface;
         // basically if netlatest elapsed is within reasonable limits (logger interval + 20%) then its from the day
         // before rollover so we can use it to replace regular elapsed
         // which is 0 when there is anew log file
         if (file_exists($netLatestLocation)) {
             $last = explode("|", file_get_contents($netLatestLocation));
             $netlatestElapsed = $timestamp - filemtime($netLatestLocation);
             //if its a new logfile check to see if there is previous netlatest data
             if ($elapsed == 0) {
                 //data needs to within the logging period limits to be accurate
                 $interval = $this->getLoggerInterval();
                 if (!$interval) {
                     $interval = 360;
                 } else {
                     $interval = $interval * 1.2;
                 }
                 if ($netlatestElapsed <= $interval) {
                     $elapsed = $netlatestElapsed;
                 }
             }
         }
         //if we were able to get last data from net latest above
         if (@$last && $elapsed) {
             $trans_diff = ($trans - $last[0]) / 1024;
             if ($trans_diff < 0) {
                 $trans_diff = (4294967296 + $trans - $last[0]) / 1024;
             }
             $trans_rate = round($trans_diff / $elapsed, 2);
             $recv_diff = ($recv - $last[1]) / 1024;
             if ($recv_diff < 0) {
                 $recv_diff = (4294967296 + $recv - $last[1]) / 1024;
             }
             $recv_rate = round($recv_diff / $elapsed, 2);
             $string = $timestamp . "|" . $trans_rate . "|" . $recv_rate . "\n";
         } else {
             //if this is the first value in the set and there is no previous data then its null
             $lastlogdata = "|0.0|0.0";
             $string = $timestamp . $lastlogdata . "\n";
         }
         //write out log data here
         LoadUtility::safefilerewrite($logfile, $string, "a", true);
         // write out last transfare and received bytes to latest
         $last_string = $trans . "|" . $recv;
         $fh = dirname($this->logfile) . DIRECTORY_SEPARATOR . "_net_latest_" . $interface;
         LoadUtility::safefilerewrite($fh, $last_string, "w", true);
         //If alerts are enabled, check for alerts
         if (@$last && $elapsed) {
             if (Alert::$alertStatus) {
                 $this->checkAlerts($timestamp, $trans_rate, $recv_rate, $settings);
             }
         }
         //figure out how to send back data for multiple interfaces here
         //echo "STRING:" . $string;
         $apiString[$interface] = $string;
         /*
         if ($apiString == "")
         else
         	$apiString += "|" . $string;
         */
     }
     if ($type == "api") {
         return $apiString;
     } else {
         return true;
     }
 }
Ejemplo n.º 12
0
 /**
  * logMemoryUsageData
  *
  * Retrives data and logs it to file
  *
  * @param string $type type of logging default set to normal but it can be API too.
  * @return string $string if type is API returns data as string
  *
  */
 public function logData($type = false)
 {
     $class = __CLASS__;
     $settings = Logger::$_settings->{$class};
     //get database settings
     //need some error checking here ie  return if they are empty
     $mysqlserver = $settings['settings']['mysqlserver'];
     $mysqluser = $settings['settings']['mysqluser'];
     $mysqlpassword = $settings['settings']['mysqlpassword'];
     //test database connection
     $connection = mysqli_connect($mysqlserver, $mysqluser, $mysqlpassword);
     if (mysqli_connect_errno()) {
         echo "Failed to connect to MySQL: " . mysqli_connect_error();
         return false;
     }
     $query1 = mysqli_query($connection, "SHOW GLOBAL STATUS LIKE 'Bytes_received'");
     $query2 = mysqli_query($connection, "SHOW GLOBAL STATUS LIKE 'Bytes_sent'");
     $query3 = mysqli_query($connection, "SHOW GLOBAL STATUS LIKE 'Queries'");
     //write the results
     $row = mysqli_fetch_array($query1);
     $bytesReceived = $row[1];
     $row = mysqli_fetch_array($query2);
     $bytesSent = $row[1];
     $row = mysqli_fetch_array($query3);
     $queries = $row[1];
     //free up querys and connections
     mysqli_free_result($query1);
     mysqli_free_result($query2);
     mysqli_free_result($query3);
     mysqli_close($connection);
     //for debugging
     //echo 'DATA READ   : ' . $bytesReceived . '|' . $bytesSent . '|' . $queries . "\n";
     //grab the logfile
     $logfile = sprintf($this->logfile, date('Y-m-d'));
     if ($logfile && file_exists($logfile)) {
         $elapsed = time() - filemtime($logfile);
     } else {
         $elapsed = 0;
     }
     //meaning new logfile
     //used to help calculate the difference as mysql charts is thruput not value based
     //this data is stored in _mysql_latest
     // grab net latest location and figure out elapsed
     $mysqllatestElapsed = 0;
     $mysqlLatestLocation = dirname($logfile) . DIRECTORY_SEPARATOR . '_mysql_latest';
     // basically if mysqllatestElapsed is within reasonable limits (logger interval + 20%) then its from the day
     // before rollover so we can use it to replace regular elapsed
     // which is 0 when there is anew log file
     $last = null;
     if (file_exists($mysqlLatestLocation)) {
         $last = explode("|", file_get_contents($mysqlLatestLocation));
         if (!isset($last[1]) || !$last[1] || $last[1] == null || $last[1] == "") {
             $last[1] = 0;
         }
         if (!isset($last[2]) || !$last[2] || $last[2] == null || $last[2] == "") {
             $last[2] = 0;
         }
         $mysqllatestElapsed = time() - filemtime($mysqlLatestLocation);
         //if its a new logfile check to see if there is previous netlatest data
         if ($elapsed == 0) {
             //data needs to within the logging period limits to be accurate
             $interval = $this->getLoggerInterval();
             if (!$interval) {
                 $interval = 360;
             } else {
                 $interval = $interval * 1.2;
             }
             if ($mysqllatestElapsed <= $interval) {
                 $elapsed = $mysqllatestElapsed;
             }
         }
     }
     //echo 'LAST STORED : ' . $last[0] . '|' . $last[1] . '|' . $last[2] . "\n";
     //if we were able to get last data from mysql latest above
     //figure out the difference as thats what we chart
     if (@$last && $elapsed) {
         $recv_diff = $bytesReceived - $last[0];
         if ($recv_diff < 0) {
             $recv_diff = 0;
         }
         $sent_diff = $bytesSent - $last[1];
         if ($sent_diff < 0) {
             $sent_diff = 0;
         }
         //$queries_diff = ($queries - $last[2]) - 4;  // we are the 4 queries! remove to be accurate really
         $queries_diff = $queries - $last[2];
         if ($queries_diff < 0) {
             $queries_diff = 0;
         }
         $string = time() . "|" . $recv_diff . "|" . $sent_diff . "|" . $queries_diff . "\n";
         //echo 'DATA WRITE  : ' . $recv_diff . '|' . $sent_diff . '|' . $queries_diff . "\n";
     } else {
         //if this is the first value in the set and there is no previous data then its null
         $lastlogdata = "|0.0|0.0|0.0";
         $string = time() . $lastlogdata . "\n";
     }
     //write out log data here
     LoadUtility::safefilerewrite($logfile, $string, "a", true);
     // write out last transfare and received bytes to latest
     $last_string = $bytesReceived . "|" . $bytesSent . "|" . $queries;
     $fh = dirname($this->logfile) . DIRECTORY_SEPARATOR . "_mysql_latest";
     LoadUtility::safefilerewrite($fh, $last_string, "w", true);
     if ($type == "api") {
         return $string;
     } else {
         return true;
     }
 }
Ejemplo n.º 13
0
 public function logData($type = false)
 {
     $class = __CLASS__;
     $settings = Logger::$_settings->{$class};
     $timestamp = time();
     $core_nums = trim(exec("grep -P '^processor' /proc/cpuinfo|wc -l"));
     //echo 'PROCS: ' . $core_nums . "\n";
     $procStats = array();
     //get the processor stats for primary cpu
     if (!$this->getProcStats($procStats, 0)) {
         return false;
     }
     //we just need the first 4 values of procStats to track cpu usage
     //$totalUsed = $procStats[0] + $procStats[1] + $procStats[2] + $procStats[3];
     ////////////////////////////////////////////////////////////////
     //now start the logging
     //grab the logfile
     $logfile = sprintf($this->logfile, date('Y-m-d'));
     $separator = "_proc_latest";
     if ($logfile && file_exists($logfile)) {
         $elapsed = time() - filemtime($logfile);
     } else {
         $elapsed = 0;
     }
     //meaning new logfile
     //used to help calculate the difference as proc chart data is thruput not value based
     //this data is stored in _proc_latest
     // grab net latest location and figure out elapsed
     $mysqllatestElapsed = 0;
     $mysqlLatestLocation = dirname($logfile) . DIRECTORY_SEPARATOR . $separator;
     // basically if mysqllatestElapsed is within reasonable limits (logger interval + 20%) then its from the day
     // before rollover so we can use it to replace regular elapsed
     // which is 0 when there is anew log file
     $last = null;
     if (file_exists($mysqlLatestLocation)) {
         $last = explode("|", file_get_contents($mysqlLatestLocation));
         if (!isset($last[1]) || !$last[1] || $last[1] == null || $last[1] == "") {
             $last[1] = $last[2] = $last[3] = $last[4] = 0;
         }
         $mysqllatestElapsed = time() - filemtime($mysqlLatestLocation);
         //if its a new logfile check to see if there is previous netlatest data
         if ($elapsed == 0) {
             //data needs to within the logging period limits to be accurate
             $interval = $this->getLoggerInterval();
             if (!$interval) {
                 $interval = 360;
             } else {
                 $interval = $interval * 1.2;
             }
             if ($mysqllatestElapsed <= $interval) {
                 $elapsed = $mysqllatestElapsed;
             }
         }
     }
     //echo 'LAST STORED : ' . $last[0] . '|' . $last[1] . '|' . $last[2] . '|' . $last[3] .  "\n";
     //figure out the difference as thats what we chart
     if (@$last && $elapsed) {
         $dif = array();
         $dif['user'] = $procStats[0] - $last[0];
         $dif['nice'] = $procStats[1] - $last[1];
         $dif['sys'] = $procStats[2] - $last[2];
         $dif['idle'] = $procStats[3] - $last[3];
         //store other usage as well now
         //or calculate in charts?
         $total = array_sum($dif);
         $cpu = array();
         foreach ($dif as $x => $y) {
             $cpu[$x] = round($y / $total * 100, 2);
             if ($cpu[$x] < 0) {
                 $cpu[$x] = 0;
             }
         }
         //var_dump ($cpu);
         //vlaculate other usage and store this data
         //this is processess not accounted for in the idle variable
         $cpu['other'] = round(100 - ($cpu['user'] + $cpu['nice'] + $cpu['sys'] + $cpu['idle']), 2);
         if ($cpu['other'] < 0) {
             $cpu['other'] = 0;
         }
         $string = time() . "|" . $cpu['user'] . "|" . $cpu['nice'] . "|" . $cpu['sys'] . "|" . $cpu['idle'] . "|" . $cpu['other'] . "\n";
         //echo 'STRING:' . $string;
     } else {
         //if this is the first value in the set and there is no previous data then its null
         $lastlogdata = "|0.0|0.0|0.0|0.0|0.0";
         $string = time() . $lastlogdata . "\n";
     }
     //echo 'STRING:' . $string;
     //get out other usage as idle - (user+cpu+nice)
     //$otherUsage =  100 - ($cpu['idle'] + $cpu['user'] + $cpu['nice'] + $cpu['sys']);
     //$testTotal = $cpu['user'] + $cpu['nice'] + $cpu['sys']  + $cpu['idle'] + $otherUsage;
     //echo 'TOTAL:' . $testTotal . "\n" ;
     //write out log data here
     LoadUtility::safefilerewrite($logfile, $string, "a", true);
     // write out last transfare and received bytes to latest
     $last_string = $procStats[0] . "|" . $procStats[1] . "|" . $procStats[2] . "|" . $procStats[3];
     $fh = dirname($this->logfile) . DIRECTORY_SEPARATOR . $separator;
     LoadUtility::safefilerewrite($fh, $last_string, "w", true);
     if ($type == "api") {
         return $string;
     } else {
         return true;
     }
 }