-
Notifications
You must be signed in to change notification settings - Fork 0
/
fritzbox-syslog-daemon.php
264 lines (232 loc) · 7.04 KB
/
fritzbox-syslog-daemon.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?
include_once 'SysLogEvent.php';
include_once 'FritzLogCenterEvent.php';
include_once 'FritzLogCenterRepeatedEvent.php';
include_once 'FritzLogEvent.php';
include_once 'FritzLuaLog.php';
include_once 'LogCenter.php';
include_once 'UdpLog.php';
$logVersion=0;
$repeatFilter = "/^(?<message>.*)\s+\[(?<count>[0-9]+) messages since (?<d1>[0-9]{2}).(?<d2>[0-9]{2}).(?<d3>[0-9]{2})\s+(?<ts>[0-9]{2}:[0-9]{2}:[0-9]{2})\]$/";
$logcenter_path = '/var/services/homes/admin/logs'; // Your logs are located on the volume you installed the Log Center package on.
$syslog_port = 516; // my default port is 516
//parse the commandline to get the connectiondetails for your Fritz!Box, the default username is 'stats'.
$options = parseCommandLine('viadqjl:', array("udp::","ignore::"), 'stats');
echo 'pid= ' . getmypid() . PHP_EOL;
var_dump($options);
$rundbquery = cmdLineSwitch("d",$options);
$runquery = cmdLineSwitch("q",$options);
$initDb = cmdLineSwitch("i",$options);
$verbose = cmdLineSwitch("v",$options);
if (cmdLineSwitch("j",$options)==TRUE) $logVersion=1;
setAnonymousDb($options);
$filter="";
if(array_key_exists("ignore",$options)) $filter = $options["ignore"];
if(array_key_exists("l",$options)!=TRUE and ($runquery===FALSE or $rundbquery===TRUE))
{
echo "LogCenter path must be specified" . PHP_EOL;
die();
}
$logcenter_path = $options["l"];
if(str_ends_with7($options["l"].'/','//'))
{
$logcenter_path = substr($options["l"],0,strlen($options["l"])-1);
};
//echo $logcenter_path.PHP_EOL;
if(array_key_exists("udp",$options)) $syslog_port = intval($options["udp"]);
$log = new UdpLog("127.0.0.1", $syslog_port);
$log->facility(0)->procid(1)->hostname($fritz_host);
if ($initDb===TRUE)
{
$ts = new DateTime();
$log->appname("SysLogDaemon")->info($ts->format('c'),"Database initialization message");
echo 'Database should be created from syslog packet...' . PHP_EOL;
sleep(5);
removeDbInitMarker($logcenter_path,$fritz_host);
die();
}
if ($rundbquery===TRUE)
{
$rowcount=-1;
try
{
// sudo php7x ./fritzbox-syslog-daemon.php -d -l=/var/services/homes/admin/logs
$rowcount = getLogCenterCount($logcenter_path,$fritz_host);
}
catch(Exception $e)
{
echo 'Failed to open the database to get the record count of the logs table. Insufficient privileges or file not found?'. PHP_EOL;
echo 'Caught exception: '. $e->getMessage() . PHP_EOL;
}
var_dump($rowcount);
die();
}
// prepare the session
$fritz = new FritzLuaLog($fritz_host, $fritz_pwd, $fritz_user, $transport);
if ($runquery===TRUE)
{
if ($fritz->login())
{
var_dump($fritz->getlogs($filter));
}
else
{
echo "Login failed" . PHP_EOL;
}
$fritz = null;
die();
}
if ($fritz->login())
{
$existing = getLogCenterTimeStamps($logcenter_path,$fritz_host);
if (count($existing)==0)
{
$samples = array();
$ts_samples =array();
for($i=0;$i<20;$i++)
{
if($i==0)sleep(5);
array_push($samples, $fritz->getlogs($filter));
}
foreach($samples as $sample)
{
$row = $sample->Log[count($sample->Log)-1];
array_push($ts_samples, $row->ts());
}
foreach($samples as $sample)
{
$row = $sample->Log[count($sample->Log)-1];
if ($row->ts()==max($ts_samples))
{
foreach ($sample->Log as $row)
{
$log->appname($row->category())->msgid($row->id())->info($row->tslog(),$row->message());
}
break;
}
}
sleep(5);
$existing = getLogCenterTimeStamps($logcenter_path,$fritz_host);
}
$existing = getLogCenterTail($logcenter_path,$fritz_host);
echo 'Starting loop...' . PHP_EOL;
while(TRUE)
{
$data = $fritz->getlogs($filter);
if ($data===FALSE)
{
$fritz->login();
}
else
{
$diff = array_udiff( $data->Log, $existing, 'logEventComparison');
usort($diff,'logEventSortOrder');
$added = FALSE;
foreach ($diff as $row)
{
if($added===FALSE) $added = $row->ts();
//echo 'adding ts '.$row->ts().PHP_EOL;
$log->appname($row->category())->msgid($row->id())->info($row->tslog(),$row->message());
}
sleep(300);
if($added===FALSE)
{
//no changes
}
else
{
$tail = getLogCenterTailFromTime($logcenter_path,$fritz_host,$added);
$added = array();
$addedKeys = array();
foreach($tail as $row)
{
array_push($added, $row);
array_push($addedKeys, $row->key());
}
array_unique($addedKeys,SORT_STRING);
$uniqueTail=array_fill_keys($addedKeys,NULL);
foreach($tail as $row)
{
if($uniqueTail[$row->key()]===NULL){
$uniqueTail[$row->key()] = $row;
}
else
{ // we should not see this message
echo 'Warning: message duplicated by last fetch from device' .PHP_EOL;
}
}
$uniqueTail=NULL;
$added=NULL;
$addedKeys=NULL;
// now filter out duplicates of repeated messages we captured before
$repeats = array();
$repeatKeys = array();
foreach(getLogCenterTail($logcenter_path,$fritz_host) as $row)
if (preg_match($repeatFilter, $row->message(), $match, PREG_UNMATCHED_AS_NULL)===1)
{
$repeat = new FritzLogCenterRepeatedEvent($logcenter_path,$fritz_host,$match,$row);
array_push($repeats, $repeat);
array_push($repeatKeys, $repeat->key());
}
array_unique($repeatKeys,SORT_STRING);
$lastRepeats=array_fill_keys($repeatKeys,NULL);
foreach($repeats as $repeat)
{
if($lastRepeats[$repeat->key()]===NULL){
$lastRepeats[$repeat->key()] = $repeat;
}
else if ($lastRepeats[$repeat->key()]->count() < $repeat->count()){
$lastRepeats[$repeat->key()] = $repeat;
}
}
foreach($repeats as $repeat)
{
if($lastRepeats[$repeat->key()]->count() != $repeat->count()){
if ($verbose===TRUE)
{
echo 'message #'.$repeat->count().' superseded by #'.$lastRepeats[$repeat->key()]->count() .PHP_EOL;
echo 'message #'.$repeat->count().': id '.$repeat->id().' ';
}
removeLogCenterRepeatEvent($logcenter_path,$fritz_host, $repeat->id(),$verbose);
if ($repeat->firstId()!=NULL){
if ($verbose===TRUE) echo 'first message superseded by #'.$lastRepeats[$repeat->key()]->count().PHP_EOL;
removeLogCenterRepeatEvent($logcenter_path,$fritz_host, $repeat->firstId(),$verbose);
if ($verbose===TRUE)echo' firstId:'.$repeat->firstId();
}
if ($verbose===TRUE)echo PHP_EOL;
}
}
}
}
$existing = getLogCenterTail($logcenter_path,$fritz_host);
}
}
else
{
echo "Login failed" . PHP_EOL;
}
$fritz = null;
$log=null;
function logEventComparison(SysLogEvent $rte, SysLogEvent $dbe)
{
$db = $dbe->category().$dbe->ts().$dbe->message();
$rt = $rte->category().$rte->ts().$rte->message();
return strcmp($db,$rt);
}
function logEventSortOrder(SysLogEvent $a, SysLogEvent $b)
{
return ($a->ts() - $b->ts());
}
function setAnonymousDb($options)
{
global $dsm;
$dsm = (TRUE === cmdLineSwitch("a",$options))? 7 : 6;
}
function str_ends_with7(string $haystack, string $needle):bool
{
if(strlen($needle)>strlen($haystack))return FALSE;
if(strlen($needle)==strlen($haystack))return $haystack===$needle;
$hay=substr($haystack,strlen($haystack)-strlen($needle),strlen($needle));
return $needle===$hay;
}
?>