Skip to content

Instantly share code, notes, and snippets.

@nikiink
Last active August 29, 2015 14:22
Show Gist options
  • Save nikiink/6ea1d69f347704bf5d24 to your computer and use it in GitHub Desktop.
Save nikiink/6ea1d69f347704bf5d24 to your computer and use it in GitHub Desktop.
Upsmon actions scheduler by eventlog. Start scheduled tasks based on the events logged on the eventlog file by Upsmon.
<?php
/*
PHPTail class from:
https://github.com/taktos/php-tail
https://code.google.com/p/php-tail/
*/
class PHPTail {
/**
* Location of the log file we're tailing
* @var string
*/
private $log = "";
/**
* This variable holds the maximum amount of bytes this application can load into memory (in bytes).
* @var string
*/
private $maxSizeToLoad;
/**
*
* PHPTail constructor
* @param string $log the location of the log file
* @param integer $defaultUpdateTime The time between AJAX requests to the server.
* @param integer $maxSizeToLoad This variable holds the maximum amount of bytes this application can load into memory (in bytes). Default is 2 Megabyte = 2097152 byte
*/
public function __construct($log, $maxSizeToLoad = 2097152) {
$this->log = $log;
$this->maxSizeToLoad = $maxSizeToLoad;
}
/**
* This function is in charge of retrieving the latest lines from the log file
* @param string $lastFetchedSize The size of the file when we lasted tailed it.
* @return Returns an array with latest file size ('size' key) and appended lines ('data' key).
*/
public function getNewLines($lastFetchedSize) {
/**
* Clear the stat cache to get the latest results
*/
clearstatcache();
/**
* Define how much we should load from the log file
* @var
*/
$fsize = filesize($this->log);
$maxLength = ($fsize - $lastFetchedSize);
/**
* Verify that we don't load more data then allowed.
*/
if($maxLength > $this->maxSizeToLoad) {
return json_encode(array("size" => $fsize, "data" => array("ERROR: PHPTail attempted to load more (".round(($maxLength / 1048576), 2)."MB) then the maximum size (".round(($this->maxSizeToLoad / 1048576), 2)."MB) of bytes into memory. You should lower the defaultUpdateTime to prevent this from happening. ")));
}
/**
* Actually load the data
*/
$data = array();
if($maxLength > 0) {
$fp = fopen($this->log, 'r');
fseek($fp, -$maxLength , SEEK_END);
$data = explode("\n", fread($fp, $maxLength));
}
/**
* If the last entry in the array is an empty string lets remove it.
*/
if(end($data) == "") {
array_pop($data);
}
return array('size' => $fsize, 'data' => $data);
}
}
class UpsmonActionsScheduler {
public function __construct($eventLogPath) {
$tail = new PHPTail($eventLogPath);
$lastSize = filesize($eventLogPath);
while (true) {
$result = $tail->getNewLines($lastSize);
$lastSize = $result['size'];
$this->processEventLogRows($result['data']);
usleep(500000); //500 ms
}
}
public function processEventLogRows($rows) {
//10.06.2015 08:39:04 Stop UpsAgent.
//10.06.2015 08:39:08 Start UpsAgent.
//10.06.2015 08:39:10 UPS 01 00 Communication is lost. [N]
//10.06.2015 08:39:12 UPS 01 01 Communication is established.
foreach ($rows as $row) {
//echo "$row\n";
$event_code_description = substr($row, 37);
//$parti = explode(" ", $codice_descrizione_evento);
if (strlen($event_code_description) > 5) {
$event_code = trim(substr($event_code_description,0,2));
$event_description = trim(substr($event_code_description,3));
// Removing [X] at the end of event description (if exists)
$bracket_pos = strrpos($event_description, "[");
if ($bracket_pos) {
$event_description = trim(substr($event_description,0,$bracket_pos));
}
if (strlen($event_code) > 0) {
//echo "[" . $event_code . "] ";
//echo "[" . $event_description . "]";
$task_name = "UPSMON_" . $event_description;
// Start Scheduled Task named UPSMON_<event_description>
$command_line = "schtasks /Run /TN " . escapeshellarg($task_name);
echo "start task: $command_line\n";
exec($command_line, $cmd_outputs, $exit_code);
//echo "start task exit code: $exit_code"; //exit code
//echo "start task output: " . print_r($cmd_outputs, true) . "\n"; //output
}
}
}
}
}
new UpsmonActionsScheduler("C:/ProgramData/Upsmon/event.log");
@nikiink
Copy link
Author

nikiink commented Jun 12, 2015

The script checks appended lines to the file C:\ProgramData\Upsmon\event.log. If is appended an event line the script starts the scheduled task with name UPSMON_<event_description>.

For example:

10.06.2015 08:39:04                     Stop UpsAgent.
10.06.2015 08:39:08                     Start UpsAgent.
10.06.2015 08:39:10 UPS 01           00 Communication is lost. [N]
10.06.2015 08:39:12 UPS 01           01 Communication is established.

These tasks will run
UPSMON_Communication is lost.
UPSMON_Communication is established.

event lines are different from others having the 2 digit event code (00, 01, ...) before the description.
Pay attention that the leading [X] characters are removed from event description, so if the description is for example "Communication is lost. [N]" the task started will be ""Communication is lost.".

@nikiink
Copy link
Author

nikiink commented Jun 12, 2015

This script is useful if Upsmon is unable to start itself any executable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment