Bei der Ausführung des Scripts wird die Zeile „Zeitraum von / bis“ oberhalb der Filteroptionen eingefügt.
Um ein Beispiel eines selektierten Zeitraum einzustellen, wird bei der Installation der Default-Wert auf 07:00 - 20:00 Uhr gesetzt.
Die Bedienung ist gleich eines Wochenplans. Klick auf das Uhrensymbol in der Zeile „Zeitraum von / bis“ rechts außen …
Ich mußte einen Unterschied zwischen den IPS Versionen 4.10 und 4.40 feststellen (Datum und Zeitangabe in der Logfile).
Sollten weitere Unterschiede in neueren Versionen oder Umgebungen (z.B. Linux) auftreten kann ich das nicht nachstellen.
Aus Berechtigungsgründen kann das aktuelle Script auf der ersten Seite des Beitrag von mir nicht aktualisiert werden.
<?
################################################################################
# Scriptbezeichnung: System.StatusLog.ips.php
# Version: 1.2
# Author: Heiko Wilknitz (@Pitti) kleine Ergänzungen N22 + BESTEX
#
# Dieses Skript filtert aus dem IPS Logfile die gewünschten Einträge:
# 0:ALL = (ERROR, WARNING, CUSTOM, NOTIFY, SUCCESS)
# 1:ERROR
# 2:WARNING
# 3:CUSTOM
# 4:NOTIFY
# 5:SUCCESS
#
# Installation:
# -------------
#
# HINWEIS: NUR FÜR LINUX BASIIERTE SYSTEM!!!
#
# Dieses Skript richtet automatisch alle nötigen Objekte bei manueller
# Ausführung ein.
#
# - Neues Skript erstellen
# - Diesen PHP-Code hineinkopieren
# - Abschnitt 'Konfiguration' den eigenen Gegebenheiten anpassen
# - Skript Abspeichern
# - Skript Ausführen
#
# ------------------------------ Konfiguration ---------------------------------
#
# Logfile, sollte nichts geändert werden
$logDir = IPS_GetLogDir();
$logFile = 'logfile.log';
#
# A fast and powerful alternative to grep (https://github.com/svent/sift)
$useSift = false;
#
# Filter Profil
$filter = array(
array(0,'ALL', '', 8421631),
array(1,'ERROR', '', 16744576),
array(2,'WARNING','', 16777088),
array(3,'SUCCESS','', 8454016),
array(4,'NOTIFY', '', 16744703),
array(5,'CUSTOM', '', 12632256)
);
#
# Hier ggf. die HTML Ausgabe anpassen
$maxlines = 1500; /* Maximale Anzahl Zeilen in der HTML Tabellenanzeige Wird nur initialisiert wenn dieses script in der Konsole ausgeführt wird. Speichern reicht nicht !*/
$pgdwntrg = 10; /* Angabe in Sekunden - innerhalb dieser Zeitangabe muß die Auswahl erneut gedrückt werden um auf die nächste Tabellenseite zu blättern */
################################################################################
// INSTALLATION
if ($_IPS['SENDER']=='Execute') {
$pos = 0;
// Filter
$vpn = "IPS.Logging";
$vid = CreateVariableByName($_IPS['SELF'], "Filter", 1);
CreateProfileInteger($vpn, 'CloseAll', '', '', 0, 0, 0, 0, $filter);
IPS_SetVariableCustomProfile($vid, $vpn);
IPS_SetVariableCustomAction($vid, $_IPS['SELF']);
IPS_SetPosition($vid, $pos++);
SetValue($vid, 0);
// select CUSTOM
$vid = CreateVariableByName($_IPS['SELF'], "Select Sender", 3);
if (IPS_GetKernelVersion()< "5.0"){IPS_SetVariableCustomProfile($vid, '~String');}else{IPS_SetVariableCustomProfile($vid, '');}
IPS_SetIcon($vid, "Gear");
IPS_SetVariableCustomAction($vid, $_IPS['SELF']);
IPS_SetPosition($vid, $pos++);
SetValue($vid, '');
// select ALL
$vid = CreateVariableByName($_IPS['SELF'], "Message Filter", 3);
if (IPS_GetKernelVersion()< "5.0"){IPS_SetVariableCustomProfile($vid, '~String');}else{IPS_SetVariableCustomProfile($vid, '');}
IPS_SetIcon($vid, "Gear");
IPS_SetVariableCustomAction($vid, $_IPS['SELF']);
IPS_SetPosition($vid, $pos++);
SetValue($vid, '');
// Messages
$vid = CreateVariableByName($_IPS['SELF'], "LogMessages", 3);
IPS_SetVariableCustomProfile($vid, '~HTMLBox');
IPS_SetIcon($vid, "Database");
IPS_SetPosition($vid, $pos++);
// select from / to
$eid = CreateEventByName($_IPS['SELF'], "Zeitraum von / bis");
IPS_SetPosition($eid, -1);
// LogMessages Info ***********
IPS_SetInfo($vid, json_encode(array('key' => '', 'timestamp' => time(), 'maxlines' => $maxlines, 'lastkey' => '', 'lastline' => '', 'pgdwntrg' => $pgdwntrg,'filteropt'=>$filter, 'eid'=>$eid, )));
// $LogMsgProp=array('key' => '', 'timestamp' => time(), 'maxlines' => '', 'pgdwntrg' => '');
}
// AKTION VIA WEBFRONT
else if ($_IPS['SENDER'] == "WebFront") {
// Speichern
SetValue($_IPS['VARIABLE'],$_IPS['VALUE'] );
$filter_key = GetValueInteger( IPS_GetObjectIDByName ("Filter",$_IPS['SELF'] ));
$sender = GetValueString( IPS_GetObjectIDByName ("Select Sender",$_IPS['SELF'] ));
$message_key_word = GetValueString( IPS_GetObjectIDByName ("Message Filter",$_IPS['SELF'] ));
if($filter_key == 5)
{
IPS_SetHidden ( IPS_GetObjectIDByName ("Select Sender",$_IPS['SELF'] ), false);
}
else
{
IPS_SetHidden ( IPS_GetObjectIDByName ("Select Sender",$_IPS['SELF'] ), true);
}
if($filter_key == 0)
{
IPS_SetHidden ( IPS_GetObjectIDByName ("Message Filter",$_IPS['SELF'] ), false);
}
else
{
IPS_SetHidden ( IPS_GetObjectIDByName ("Message Filter",$_IPS['SELF'] ), true);
}
// Build command line
$cmd = BuildExecute($useSift, $filter, $filter_key, $logDir.$logFile);
// Filtern
$html = GetLogMessages($cmd);
// Anzeigen
$vid = CreateVariableByName($_IPS['SELF'], "LogMessages", 3);
SetValue($vid, $html);
// Objekt Info aktualisieren
$objArray=IPS_GetObject($vid);
$LogMsgInf = json_decode($objArray['ObjectInfo'], true);
$LogMsgInf['key'] = $filter_key;
IPS_SetInfo($vid, json_encode($LogMsgInf));
}
# -------------------------------- FUNKTIONEN ----------------------------------
// Kommandozeile zusammenbauen
function BuildExecute($fast, $filter, $index, $log)
{
$cmd = '';
$len = count($filter);
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
//echo 'This is a server using Windows!';
// Beispiel findstr "SUCCESS ERROR" logfile.log
$cmd = 'findstr "';
// ALL
if($index == 0) {
foreach($filter as $key => $arr) {
if ($key == 0) continue;
$cmd .= ' '.$arr[1].' ';
if ($key != $len - 1) $cmd .= ' ';
}
}
// SPECIFIC
else {
$cmd .= ' '.$filter[$index][1].' ';
}
$cmd .= '" ';
// echo "Befehl: ".$cmd."
";
} else {
/* Linux - Anfang */
//SIFT => $cmd = 'sift -e " ERROR " -e " WARNING " -e " CUSTOM " -e " NOTIFY " -e " SUCCESS " '.$log;
if($fast == true) {
$cmd = 'sift ';
// ALL
if($index == 0) {
foreach($filter as $key => $arr) {
if ($key == 0) continue;
$cmd .= '-e " '.$arr[1].' "';
if ($key != $len - 1) $cmd .= ' ';
}
}
// SPECIFIC
else {
$cmd .= '-e " '.$filter[$index][1].' "';
}
$cmd .= ' ';
}
//GREP => $cmd = 'grep -E "( ERROR | WARNING | CUSTOM | NOTIFY | SUCCESS )" '.$log
else {
$cmd = 'grep -E "(';
// ALL
if($index == 0) {
foreach($filter as $key => $arr) {
if ($key == 0) continue;
$cmd .= ' '.$arr[1].' ';
if ($key != $len - 1) $cmd .= '|';
}
}
// SPECIFIC
else {
$cmd .= ' '.$filter[$index][1].' ';
}
$cmd .= ')" ';
}
/* Linux - ende */
}
$cmd .= $log;
return $cmd;
};
// Daten über alle RSSI_DEVICE(s) holen und rendern
function GetLogMessages($cmd)
{
global $filter_key,$sender,$message_key_word;
// Anzeige aufbereiten
$style = "";
$style = $style.'<style type="text/css">';
$style = $style.'table {border-collapse: collapse; font-size: 14px; width: 100%; }';
$style = $style.'td.fst {vertical-align: middle; text-align: left; padding: 5px; border-left: 1px solid rgba(255, 255, 255, 0.2); border-top: 1px solid rgba(255, 255, 255, 0.1); }';
$style = $style.'td.mid {vertical-align: middle; text-align: left; padding: 5px; border-top: 1px solid rgba(255, 255, 255, 0.1); }';
$style = $style.'td.lst {vertical-align: middle; text-align: left; padding: 5px; border-right: 1px solid rgba(255, 255, 255, 0.2); border-top: 1px solid rgba(255, 255, 255, 0.1); }';
$style = $style.'tr:last-child {border-bottom: 1px solid rgba(255, 255, 255, 0.2); }';
$style = $style.'tr:nth-child(even) { background-color: rgba(0, 0, 0, 0.2); }';
$style = $style.'.th { color: rgb(255, 255, 255); background-color: rgb(160, 160, 0); font-weight:bold; background-image: linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -o-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -moz-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -webkit-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); background-image: -ms-linear-gradient(top,rgba(0,0,0,0) 0,rgba(0,0,0,0.3) 50%,rgba(0,0,0,0.3) 100%); }';
$style = $style.'.dotE { width: 15px; height: 15px; clear: both; background: #ff8080; border-radius: 50%; margin-top: 1px; margin-left:5px; }';
$style = $style.'.dotW { width: 15px; height: 15px; clear: both; background: #ffff80; border-radius: 50%; float:left; margin-top: 2px; margin-right:10px; }';
$style = $style.'.dotS { width: 15px; height: 15px; clear: both; background: #80ff80; border-radius: 50%; float:left; margin-top: 2px; margin-right:10px; }';
$style = $style.'.dotC { width: 15px; height: 15px; clear: both; background: #c0c0c0; border-radius: 50%; float:left; margin-top: 2px; margin-right:10px; }';
$style = $style.'.dotN { width: 15px; height: 15px; clear: both; background: #ff80ff; border-radius: 50%; float:left; margin-top: 2px; margin-right:10px; }';
$style = $style.'</style>';
// HTML zusammenbauen
$html = $style;
// Exceute Command
$ret = exec($cmd, $out, $rc);
$html .= '<table>';
$html .= '<tr><td class="fst th" style="width:30px;">Typ</td><td class="mid th" style="width:150px;">Uhrzeit</td><td class="mid th" style="width:120px;">Sender</td><td class="lst th">Meldung</td></tr>';
$out = array_reverse($out);
$vid = CreateVariableByName($_IPS['SELF'], "LogMessages", 3);
$objArray=IPS_GetObject($vid);
$LogMsgInf = json_decode($objArray['ObjectInfo'], true);
// HTML Tabellenanzeige definieren
if ($LogMsgInf['lastkey'] == $LogMsgInf['key']){
if ((time()-$LogMsgInf['pgdwntrg']) < $LogMsgInf['timestamp']){
$firstline = $LogMsgInf['lastline']+1;
$lastline = $firstline + $LogMsgInf['maxlines'];
}else{
$firstline = 0;
$lastline = $firstline + $LogMsgInf['maxlines'];
}
}else{
$firstline = 0;
$lastline = $firstline + $LogMsgInf['maxlines'];
}
$outIdx =0;
/* 'Zeitraum von / bis' abfragen */
$eventproperties = IPS_GetEvent($LogMsgInf['eid']);
IPS_SetEventActive($LogMsgInf['eid'],false); /* Zeitanzeige im WebFront ausblenden (für evtl. zur Verwirrung) */
foreach ($eventproperties['ScheduleActions'] as $action){
if ($action['Name'] == '+'){$vonID = $action['ID'];}
if ($action['Name'] == '-'){$bisID = $action['ID'];}
}
foreach ($eventproperties['ScheduleGroups'] as $Group){
$von = '00:00:00'; /* default value */
foreach($Group['Points'] as $Point) {
if($Point['ActionID'] == $vonID){
$von=sprintf("%'.02d", $Point['Start']['Hour']).":".sprintf("%'.02d", $Point['Start']['Minute']).":".sprintf("%'.02d", $Point['Start']['Second']);
$bis='23:59:59';
$vonbisArray=array('von'=>$von,'bis'=>$bis);
}
if($Point['ActionID'] == $bisID){
$bis = sprintf("%'.02d", $Point['Start']['Hour']).":".sprintf("%'.02d", $Point['Start']['Minute']).":".sprintf("%'.02d", $Point['Start']['Second']);
$vonbisArray=array('von'=>$von,'bis'=>$bis);
}
}
}
$IPSver = IPS_GetKernelVersion(); /* das Format der Logfile ist je nach Version unterschiedlich */
foreach ($out as $line) {
$array = explode("|", $line);
if($IPSver > "4.10"){
if(substr($array[0],11,8) >= $von and substr($array[0],11,8) <= $bis){} else {continue;} /* Zeile innerhalb des 'Zeitraum von / bis' */
}else{
if($array[0] >= $von and $array[0] <= $bis){} else {continue;} /* Zeile innerhalb des 'Zeitraum von / bis' */
}
if(array_key_exists(1 ,$LogMsgInf['filteropt'][$filter_key]))
{
if(array_key_exists(2,$array))
{
// zusätzlicher Filter in der Tabellenausgabe. Es wird z.B. bei der Auswahl ERROR keine Ausgabe gemacht wenn ERROR in einer Meldung steht.
if($LogMsgInf['filteropt'][$filter_key][1] != 'ALL' )
{
if( $LogMsgInf['filteropt'][$filter_key][1] != trim($array[2]))
{continue;}
}
if(
($LogMsgInf['filteropt'][$filter_key][1] == 'CUSTOM' )
AND
($sender != '')
)
{
if( !strpos($array[3], $sender))
{continue;}
}
if(
($LogMsgInf['filteropt'][$filter_key][1] == 'ALL' )
AND
($message_key_word != '')
)
{
if( !strpos($array[4], $message_key_word))
{continue;}
}
// Check LOG in LOG :-(
if( trim($array[2]) == 'MESSAGE') continue;
If( $outIdx >= $firstline and $outIdx <= $lastline){
$color = trim($array[2]);
$html .= '<tr><td class="fst"><div class="dot'.$color[0].'"></div>'.'</td><td class="mid">'.$array[0].'</td><td class="mid">'.$array[3].'</td><td class="lst">'.$array[4].'</td></tr>';
}
if($outIdx>=$lastline) {
// $maxlines erreicht. Ausgabe letzte Tabellenzeile
$html .= '<tr><td class="fst"><div class="dot'.$color[0].'"></div>'.'</td><td class="mid">*</td><td class="mid">*</td><td class="lst"> *** max. Tabellengröße von '. $LogMsgInf['maxlines'] .' Zeilen erreicht ***<br />*** erneutes anklicken der gleichen Auswahl innerhalb von '.$LogMsgInf['pgdwntrg'].' Sek. zeigt die nächsten '. $LogMsgInf['maxlines'] .' Zeilen an ***</td></tr>';
break ;
}
}
}
$outIdx++;
}
$html.='</table>';
// LogMsgInf aktualisieren
$LogMsgInf['timestamp'] = time();
$LogMsgInf['lastline'] = $lastline;
$LogMsgInf['lastkey'] = $LogMsgInf['key'];
IPS_SetInfo($vid, json_encode($LogMsgInf));
// Zurück
return $html;
}
// Erzeugt eine Variable unterhalb {id} mit dem Namen {name} vom Typ [type}
// Existiert die Variable schon wird diese zurückgeliefert.
// Types: 0 = Boolean, 1 = Integer, 2 = Float, 3 = String
function CreateVariableByName($id, $name, $type)
{
$vid = @IPS_GetVariableIDByName($name, $id);
if($vid===false) {
$vid = IPS_CreateVariable($type);
IPS_SetParent($vid, $id);
IPS_SetName($vid, $name);
}
return $vid;
}
// Erzeugt ein Ereignis unterhalb {id} mit dem Namen {name}
// Existiert das Ereignis schon wird diese zurückgeliefert.
function CreateEventByName($id, $name)
{
$eid = @IPS_GetEventIDByName($name, $id);
if($eid===false) {
//Wochenplan Ereignis erstellen
$eid = IPS_CreateEvent(2);
IPS_SetEventActive($eid,false);
IPS_SetParent($eid, $id);
IPS_SetName($eid, $name);
//Anlegen von Aktionen
IPS_SetEventScheduleAction($eid, 1, "-", 1962342,""); /* abwählen */
IPS_SetEventScheduleAction($eid, 2, "+", 11942329,""); /* auswählen */
//Anlegen einer Gruppe
IPS_SetEventScheduleGroup($eid, 0, 127);
//Anlegen von Schaltpunkten für Gruppe
IPS_SetEventScheduleGroupPoint ($eid, 0, 1, 0, 0, 1, 1 ); /* abwählen ab 00:00:01 Uhr */
IPS_SetEventScheduleGroupPoint ($eid, 0, 2, 7, 0, 0, 2 ); /* auswählen ab 07:00:00 Uhr */
IPS_SetEventScheduleGroupPoint ($eid, 0, 3, 20, 0, 0, 1 ); /* abwählen ab 20:00:00 Uhr */
}
return $eid;
}
// Erzeugt ein Variablenprofil vom Typ {type} mit Name n{name}
function CreateProfile($name, $type)
{
if(!IPS_VariableProfileExists($name)) {
IPS_CreateVariableProfile($name, $type);
}
else {
$profile = IPS_GetVariableProfile($name);
if($profile['ProfileType'] != $type)
throw new Exception("Variable profile type does not match for profile ".$name);
}
}
// Erzeugt ein Integer-Variablenprofil
function CreateProfileInteger($name, $icon, $prefix, $suffix, $minvalue, $maxvalue, $step, $digits, $asso = NULL)
{
CreateProfile($name, 1);
IPS_SetVariableProfileIcon($name, $icon);
IPS_SetVariableProfileText($name, $prefix, $suffix);
IPS_SetVariableProfileDigits($name, $digits);
if(($asso !== NULL) && (sizeof($asso) !== 0)){
$minvalue = 0;
$maxvalue = 0;
}
IPS_SetVariableProfileValues($name, $minvalue, $maxvalue, $step);
if(($asso !== NULL) && (sizeof($asso) !== 0)){
foreach($asso as $ass) {
IPS_SetVariableProfileAssociation($name, $ass[0], $ass[1], $ass[2], $ass[3]);
}
}
}
################################################################################
?>