LOG Statistic auswertung

Achtung Läuft zur Zeit nur unter Windows !

Hier ist ein kleines Script mit dem Ihr euer LOG auf Auffälligkeiten auswerten könnt. Es erstellt nach Schlüsselwörtern Zähler variablen in denen die Anzahl der gefundenen Treffer des jeweiligen Schlüsselwortes gespeichert werden. Weiterhin wird ein Timer erstellt der die Auswertung stündlich erzeugt.


<?php
$filter =  array('| ERROR   |','| WARNING |','| SUCCESS |','| NOTIFY  |','| CUSTOM  |','| MESSAGE |','| DEBUG   |','KNX','Homematic'); // Those are the key words you want to analyse
$archive_id = 14817; //ID des Archive Handlers eintragen


if($_IPS['SENDER']=='Execute')
{
    CreateTimerByName($_IPS['SELF'],"LOG_TIMER", $time = 60, $repeat = true);
    foreach($filter as $key => $value)
    {
           CreateVariableByName($_IPS['SELF'],$value, 1);
    }
    run_statistic($filter,"DISPLAY");
}
elseif($_IPS['SENDER']== 'TimerEvent')
{
    run_statistic($filter,"STORE");
}
else 
{

    IPS_LogMessage('LOGSTAT', " Done nothing. Only Timer and Execution events allowed ");

}





// 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) 
{ 
   Global $archive_id;
   $vid = @IPS_GetVariableIDByName($name, $id); 
   if($vid===false) { 
      $vid = IPS_CreateVariable($type); 
      IPS_SetParent($vid, $id); 
      IPS_SetName($vid, $name); 
      AC_SetLoggingStatus ($archive_id, $vid,true);
      AC_SetAggregationType ($archive_id, $vid,1 );
      IPS_ApplyChanges($archive_id);
      IPS_LogMessage('LOGSTAT', " Variable created for ".$name);
   }
   return $vid; 
}



// Von Pitti
// Erzeugt einen Timer unterhalb {id} mit dem Namen {name} um die Zeit {time}
// Existiert das  Event schon wird diese zurückgeliefert.
function CreateTimerByName($id, $name, $time = 0, $repeat = true)
{
    $eid = @IPS_GetEventIDByName($name, $id);
    if(($eid===false) && ($time > 0)) {
        // zyklisches Event
        $eid = IPS_CreateEvent(1);
        IPS_SetParent($eid, $id);
        IPS_SetName($eid, $name);
        IPS_SetPosition($eid, -1);
        IPS_SetHidden($eid, true);
        if($repeat == true) {
            // tägliches Event
            IPS_SetEventCyclic($eid, 2, 1, 0, 0, 2, $time);
            IPS_SetEventCyclicDateFrom($eid, (int)date('j',$time), (int)date('n', $time),  (int)date('Y', $time));
 
        } else {
            // einmaliges Event
            IPS_SetEventCyclic($eid, 1, 0, 0, 0, 0, 0);
            IPS_SetEventCyclicDateFrom($eid, (int)date('j',$time), (int)date('n', $time),  (int)date('Y', $time));
            IPS_SetEventCyclicDateTo($eid, (int)date('j',$time), (int)date('n', $time),  (int)date('Y', $time));
            IPS_SetEventCyclicTimeFrom($eid, (int)date("H", $time), (int)date("i", $time), (int)date("s", $time));
            IPS_SetEventCyclicTimeTo($eid, (int)date("H", $time), (int)date("i", $time), (int)date("s", $time));
        }
        IPS_SetEventActive($eid, true);
    }
    return $eid;
}

function run_statistic($filter,$type)
{
    $path = 'C:\\IP-Symcon\\logs\\'; // This is the Windows path. Please change for other operating systems
    $new_input_file_name = "logfile.log"; // Thats the name of the file you want to analyse
    $new_output_file_name = "filter_20.txt"; // Thats the name of the temp file created 



    foreach($filter as $key => $value)
    {
        $new_search_term = $value;

        exec("del ".$path.$new_output_file_name); 
        exec("copy  ".$path.$new_input_file_name." ".$path.$new_output_file_name); 

        $local_cmd = 'findstr /c:"'.$new_search_term.'" '.$path.$new_input_file_name;
        $local_cmd = $local_cmd.' > '.$path.$new_output_file_name;

        exec($local_cmd); 

        exec('find /v /c "" '.$path.$new_output_file_name,  $nr_of_records);
        $nr_of_records_int  = substr($nr_of_records[1],44);

    
        $nr_of_records =  intval($nr_of_records_int);
        $variable_id = IPS_GetObjectIDByName ($value,$_IPS['SELF']  );
        if($type == "STORE")
        {
          SetValueInteger(  $variable_id,$nr_of_records);
        }
        elseif($type == "DISPLAY")
        {
            echo " ".$nr_of_records." ";
        }
        else 
        {
               IPS_LogMessage('LOGSTAT', " Error wrong Type ");

        }


    }
}