From: root Date: Tue, 19 Jun 2018 10:25:29 +0000 (+0200) Subject: Logdateien in Datenbank einfügen, wenn LogDB genutzt wird X-Git-Url: https://git.hoellein.online/?a=commitdiff_plain;h=b6de35e40c92db8f4f88f170da965489dbf05e9f;p=skripte Logdateien in Datenbank einfügen, wenn LogDB genutzt wird --- diff --git a/fhem/insertFhemFilelog2Db b/fhem/insertFhemFilelog2Db new file mode 100755 index 0000000..316259b --- /dev/null +++ b/fhem/insertFhemFilelog2Db @@ -0,0 +1,56 @@ + + * + * Maybe the error "no type found for device" will occur. + * If this is the case the device name was not found in the current table. + * You may want to check of the device name is still in use. + * If not you could remove lines with that device name manually from your logfile or + * trigger an action from that device thus it will create an entry in the current-table. + * + * This script requires a working php-cli with mysqli extension. + * + * Use at your own risk! + */ +$mysql = mysqli_connect("localhost", "fhemloguser", "fhemloguserpass", "fhem"); +$filename = $argv[1]; +$fh = fopen($filename, "r") or die("Could not open file ".$filename." for reading"); +$types = array(); +$insert = mysqli_prepare($mysql, "INSERT INTO `history` VALUES(?, ?, ?, ?, ?, ?, '');"); +while (!feof($fh)) { + $matches = array(); + $line = trim(fgets($fh)); + if ($line == "") { + continue; + } + preg_match("/^([0-9]{4}\-[0-9]{2}\-[0-9]{2})_([0-9]{2}:[0-9]{2}:[0-9]{2}) (\S*) (.*)/", $line, $matches); + if (!isset($type[$matches[3]])) { + $typerow = mysqli_fetch_assoc(mysqli_query($mysql, "SELECT `TYPE` FROM current WHERE `DEVICE` = '".mysqli_escape_string($mysql, $matches[3])."' LIMIT 1")); + if (is_array($typerow) && strlen($typerow['TYPE']) > 0) { + $type[$matches[3]] = $typerow['TYPE']; + } else { + die("no type found for device ".$matches[3]."!"); + } + } + $timestamp = $matches[1]." ".$matches[2]; + $device = $matches[3]; + $devtype = $type[$matches[3]]; + $event = $matches[4]; + $reading_value = array(); + preg_match("/(\S+): (.*)/", $event, $reading_value); + if (count($reading_value) == 0) { + $reading = "state"; + $value = $event; + } else { + $reading = $reading_value[1]; + $value = $reading_value[2]; + } + $insert->bind_param("ssssss", $timestamp, $device, $devtype, $event, $reading, $value); + $insert->execute(); +}