Browse Source

Caching, speedup, logging

Kai Kretschmann 3 years ago
parent
commit
dde1cbcde9
10 changed files with 376 additions and 50 deletions
  1. 1
    1
      .gitignore
  2. 24
    0
      admin/normalize.php
  3. 1
    0
      cache/.htaccess
  4. 0
    0
      cache/index.html
  5. 89
    5
      doc/db.sql
  6. 160
    43
      inc/lggr_class.php
  7. 51
    0
      inc/lggrcache_class.php
  8. 37
    0
      inc/lggrperf_class.php
  9. 3
    0
      index.php
  10. 10
    1
      tpl/foot.inc.php

+ 1
- 1
.gitignore View File

@@ -1,2 +1,2 @@
1 1
 t.php
2
-
2
+cache/key_*

+ 24
- 0
admin/normalize.php View File

@@ -0,0 +1,24 @@
1
+<?php
2
+
3
+spl_autoload_register(function($class) {
4
+	include __DIR__ . '/../inc/' . strtolower($class) . '_class.php';
5
+});
6
+
7
+$iCount=0;
8
+$a=array();
9
+$l = null;
10
+try {
11
+	$config = new AdminConfig();
12
+
13
+	$state = new LggrState();
14
+	$state->setLocalCall(true);
15
+
16
+	$l = new Lggr($state, $config);
17
+
18
+	$l->normalizeHosts();
19
+
20
+	$a = $l->getPerf();
21
+} catch(Exception $e) {
22
+	die($e->getMessage());
23
+} // try
24
+

+ 1
- 0
cache/.htaccess View File

@@ -0,0 +1 @@
1
+deny from all

+ 0
- 0
cache/index.html View File


+ 89
- 5
doc/db.sql View File

@@ -1,27 +1,110 @@
1
+-- --------------------------------------------------------
2
+-- Host:                         127.0.0.1
3
+-- Server Version:               5.5.43-0+deb7u1 - (Debian)
4
+-- Server Betriebssystem:        debian-linux-gnu
5
+-- HeidiSQL Version:             9.2.0.4971
6
+-- --------------------------------------------------------
7
+
1 8
 /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
2 9
 /*!40101 SET NAMES utf8mb4 */;
3 10
 /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
4 11
 /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
5 12
 
13
+-- Exportiere Struktur von Tabelle logger.hosts
14
+CREATE TABLE IF NOT EXISTS `hosts` (
15
+  `id` int(11) NOT NULL AUTO_INCREMENT,
16
+  `name` varchar(50) NOT NULL DEFAULT '0',
17
+  PRIMARY KEY (`id`),
18
+  UNIQUE KEY `name` (`name`)
19
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
20
+
21
+-- Daten Export vom Benutzer nicht ausgewählt
22
+
23
+
24
+-- Exportiere Struktur von View logger.LastHour
25
+-- Erstelle temporäre Tabelle um View Abhängigkeiten zuvorzukommen
26
+CREATE TABLE `LastHour` (
27
+	`id` BIGINT(20) NOT NULL,
28
+	`date` DATETIME NOT NULL,
29
+	`facility` ENUM('kern','user','mail','daemon','auth','syslog','lpr','news','uucp','authpriv','ftp','cron','local0','local1','local2','local3','local4','local5','local6','local7') NOT NULL COLLATE 'utf8_general_ci',
30
+	`level` ENUM('emerg','alert','crit','err','warning','notice','info','debug') NOT NULL COLLATE 'utf8_general_ci',
31
+	`host` VARCHAR(50) NOT NULL COLLATE 'utf8_general_ci',
32
+	`program` VARCHAR(50) NOT NULL COLLATE 'utf8_general_ci',
33
+	`pid` INT(10) UNSIGNED NOT NULL,
34
+	`message` TEXT NOT NULL COLLATE 'utf8_general_ci',
35
+	`idhost` INT(11) NULL
36
+) ENGINE=MyISAM;
37
+
38
+
39
+-- Exportiere Struktur von Tabelle logger.newlogs
6 40
 CREATE TABLE IF NOT EXISTS `newlogs` (
7 41
   `id` bigint(20) NOT NULL AUTO_INCREMENT,
8 42
   `date` datetime NOT NULL,
9 43
   `facility` enum('kern','user','mail','daemon','auth','syslog','lpr','news','uucp','authpriv','ftp','cron','local0','local1','local2','local3','local4','local5','local6','local7') NOT NULL,
10 44
   `level` enum('emerg','alert','crit','err','warning','notice','info','debug') NOT NULL,
11
-  `host` char(16) NOT NULL,
45
+  `host` varchar(50) NOT NULL,
12 46
   `program` varchar(50) NOT NULL,
13 47
   `pid` int(10) unsigned NOT NULL,
14 48
   `message` text NOT NULL,
49
+  `idhost` int(11) DEFAULT NULL,
15 50
   PRIMARY KEY (`id`),
16 51
   KEY `level` (`level`),
17
-  KEY `host` (`host`)
52
+  KEY `host` (`host`),
53
+  KEY `date` (`date`),
54
+  KEY `idhost` (`idhost`),
55
+  CONSTRAINT `FK_newlogs_hosts` FOREIGN KEY (`idhost`) REFERENCES `hosts` (`id`)
18 56
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='New logging table';
19 57
 
20
-CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `LastHour` AS select `logger`.`newlogs`.`id` AS `id`,`logger`.`newlogs`.`date` AS `date`,`logger`.`newlogs`.`facility` AS `facility`,`logger`.`newlogs`.`level` AS `level`,`logger`.`newlogs`.`host` AS `host`,`logger`.`newlogs`.`program` AS `program`,`logger`.`newlogs`.`pid` AS `pid`,`logger`.`newlogs`.`message` AS `message` from `logger`.`newlogs` where (`logger`.`newlogs`.`date` >= (now() - interval 1 hour));
21
-CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `Today` AS select `logger`.`newlogs`.`id` AS `id`,`logger`.`newlogs`.`date` AS `date`,`logger`.`newlogs`.`facility` AS `facility`,`logger`.`newlogs`.`level` AS `level`,`logger`.`newlogs`.`host` AS `host`,`logger`.`newlogs`.`program` AS `program`,`logger`.`newlogs`.`pid` AS `pid`,`logger`.`newlogs`.`message` AS `message` from `logger`.`newlogs` where (cast(now() as date) = cast(`logger`.`newlogs`.`date` as date));
22
-CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `Week` AS select `logger`.`newlogs`.`id` AS `id`,`logger`.`newlogs`.`date` AS `date`,`logger`.`newlogs`.`facility` AS `facility`,`logger`.`newlogs`.`level` AS `level`,`logger`.`newlogs`.`host` AS `host`,`logger`.`newlogs`.`program` AS `program`,`logger`.`newlogs`.`pid` AS `pid`,`logger`.`newlogs`.`message` AS `message` from `logger`.`newlogs` where (`logger`.`newlogs`.`date` >= (now() - interval 168 hour));
58
+-- Daten Export vom Benutzer nicht ausgewählt
59
+
60
+
61
+-- Exportiere Struktur von View logger.Today
62
+-- Erstelle temporäre Tabelle um View Abhängigkeiten zuvorzukommen
63
+CREATE TABLE `Today` (
64
+	`id` BIGINT(20) NOT NULL,
65
+	`date` DATETIME NOT NULL,
66
+	`facility` ENUM('kern','user','mail','daemon','auth','syslog','lpr','news','uucp','authpriv','ftp','cron','local0','local1','local2','local3','local4','local5','local6','local7') NOT NULL COLLATE 'utf8_general_ci',
67
+	`level` ENUM('emerg','alert','crit','err','warning','notice','info','debug') NOT NULL COLLATE 'utf8_general_ci',
68
+	`host` VARCHAR(50) NOT NULL COLLATE 'utf8_general_ci',
69
+	`program` VARCHAR(50) NOT NULL COLLATE 'utf8_general_ci',
70
+	`pid` INT(10) UNSIGNED NOT NULL,
71
+	`message` TEXT NOT NULL COLLATE 'utf8_general_ci',
72
+	`idhost` INT(11) NULL
73
+) ENGINE=MyISAM;
23 74
 
75
+
76
+-- Exportiere Struktur von View logger.Week
77
+-- Erstelle temporäre Tabelle um View Abhängigkeiten zuvorzukommen
78
+CREATE TABLE `Week` (
79
+	`id` BIGINT(20) NOT NULL,
80
+	`date` DATETIME NOT NULL,
81
+	`facility` ENUM('kern','user','mail','daemon','auth','syslog','lpr','news','uucp','authpriv','ftp','cron','local0','local1','local2','local3','local4','local5','local6','local7') NOT NULL COLLATE 'utf8_general_ci',
82
+	`level` ENUM('emerg','alert','crit','err','warning','notice','info','debug') NOT NULL COLLATE 'utf8_general_ci',
83
+	`host` VARCHAR(50) NOT NULL COLLATE 'utf8_general_ci',
84
+	`program` VARCHAR(50) NOT NULL COLLATE 'utf8_general_ci',
85
+	`pid` INT(10) UNSIGNED NOT NULL,
86
+	`message` TEXT NOT NULL COLLATE 'utf8_general_ci',
87
+	`idhost` INT(11) NULL
88
+) ENGINE=MyISAM;
89
+
90
+
91
+-- Exportiere Struktur von View logger.LastHour
92
+-- Entferne temporäre Tabelle und erstelle die eigentliche View
93
+DROP TABLE IF EXISTS `LastHour`;
94
+CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `LastHour` AS select `newlogs`.`id` AS `id`,`newlogs`.`date` AS `date`,`newlogs`.`facility` AS `facility`,`newlogs`.`level` AS `level`,`newlogs`.`host` AS `host`,`newlogs`.`program` AS `program`,`newlogs`.`pid` AS `pid`,`newlogs`.`message` AS `message`,`newlogs`.`idhost` AS `idhost` from `newlogs` where (`newlogs`.`date` >= (now() - interval 1 hour));
95
+
96
+
97
+-- Exportiere Struktur von View logger.Today
98
+-- Entferne temporäre Tabelle und erstelle die eigentliche View
99
+DROP TABLE IF EXISTS `Today`;
100
+CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `Today` AS select `newlogs`.`id` AS `id`,`newlogs`.`date` AS `date`,`newlogs`.`facility` AS `facility`,`newlogs`.`level` AS `level`,`newlogs`.`host` AS `host`,`newlogs`.`program` AS `program`,`newlogs`.`pid` AS `pid`,`newlogs`.`message` AS `message`,`newlogs`.`idhost` AS `idhost` from `newlogs` where (cast(now() as date) = cast(`newlogs`.`date` as date));
101
+
102
+
103
+-- Exportiere Struktur von View logger.Week
104
+-- Entferne temporäre Tabelle und erstelle die eigentliche View
105
+DROP TABLE IF EXISTS `Week`;
106
+CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `Week` AS select `newlogs`.`id` AS `id`,`newlogs`.`date` AS `date`,`newlogs`.`facility` AS `facility`,`newlogs`.`level` AS `level`,`newlogs`.`host` AS `host`,`newlogs`.`program` AS `program`,`newlogs`.`pid` AS `pid`,`newlogs`.`message` AS `message`,`newlogs`.`idhost` AS `idhost` from `newlogs` where (`newlogs`.`date` >= (now() - interval 168 hour));
24 107
 /*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */;
25 108
 /*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */;
26 109
 /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
110
+

+ 160
- 43
inc/lggr_class.php View File

@@ -5,15 +5,14 @@ class Lggr {
5 5
 	private $config=null;
6 6
 	private $db=null;
7 7
 	private $state=null;
8
-	private $perfTime=null;
9
-	private $perfCount=null;
8
+	private $cache=null;
9
+	private $aPerf=null;
10 10
 
11 11
 	function __construct(LggrState $state, AbstractConfig $config) {
12 12
 		$this->config = $config;
13 13
 		$this->state = $state;
14
-
15
-		$this->perfCount=0;
16
-		$this->perfTime=0;
14
+		$this->cache = new LggrCache();
15
+		$this->aPerf = array(); // of type LggrPerf objects
17 16
 
18 17
 		if(!$this->state->isLocalCall())
19 18
 			$this->checkSecurity();
@@ -43,17 +42,23 @@ class Lggr {
43 42
 	}
44 43
 
45 44
 	function getLevels() {
46
-		$this->perfCount++;
47
-		$startTime = microtime(true);
45
+		$perf = new LggrPerf();
48 46
 
49 47
 		$v = $this->getViewName();
50
-		$a = array();
51 48
 		$sql = "
52 49
 SELECT level, COUNT(*) AS c FROM $v
53 50
 GROUP BY level
54 51
 ORDER BY c DESC
55 52
 ";
56 53
 
54
+		$a = $this->cache->retrieve("levels$v");
55
+		if(null != $a) {
56
+			return $a;
57
+		} // if
58
+		$a = array();
59
+
60
+		$perf->start($sql);
61
+
57 62
 		$res = $this->db->query($sql);
58 63
 		if(false === $res) {
59 64
 			throw new Exception($this->db->error);
@@ -72,22 +77,32 @@ ORDER BY c DESC
72 77
 			$level->f = round($f, 2);
73 78
 		} // foreach
74 79
 
75
-		$this->perfTime += microtime(true)-$startTime;
80
+		$perf->stop();
81
+		$this->aPerf[] = $perf;
76 82
 
83
+		$this->cache->store("levels$v", $a);
77 84
 		return $a;
78 85
 	} // function
79 86
 
80 87
 	function getServers() {
81
-		$this->perfCount++;
82
-		$startTime = microtime(true);
88
+		$perf = new LggrPerf();
83 89
 
84 90
 		$v = $this->getViewName();
85
-		$a = array();
91
+
86 92
 		$sql = "
87
-SELECT host, COUNT(*) AS c FROM $v
88
-GROUP BY host
89
-ORDER BY c DESC
90
-";
93
+SELECT h.name as host, COUNT(*) AS c
94
+FROM $v d
95
+JOIN hosts h ON d.idhost=h.id
96
+GROUP BY h.id
97
+ORDER BY c DESC";
98
+
99
+		$a = $this->cache->retrieve("servers$v");
100
+		if(null != $a) {
101
+			return $a;
102
+		} // if
103
+		$a = array();
104
+
105
+		$perf->start($sql);
91 106
 
92 107
 		$res = $this->db->query($sql);
93 108
 		if(false === $res) {
@@ -107,14 +122,16 @@ ORDER BY c DESC
107 122
 			$host->f = round($f, 2);
108 123
 		} // foreach
109 124
 
110
-		$this->perfTime += microtime(true)-$startTime;
125
+		$perf->stop();
126
+		$this->aPerf[] = $perf;
111 127
 
128
+		$this->cache->store("servers$v", $a);
112 129
 		return $a;
113 130
 	} // function
114 131
 
115 132
 	function getLatest($from=0, $count=LggrState::PAGELEN) {
116
-		$this->perfCount += 2;
117
-		$startTime = microtime(true);
133
+		$perfSize = new LggrPerf();
134
+		$perfData = new LggrPerf();
118 135
 
119 136
 		$v = $this->getViewName();
120 137
 
@@ -124,27 +141,41 @@ SELECT * FROM $v
124 141
 ORDER BY `date` DESC
125 142
 LIMIT $from,$count";
126 143
 
144
+		$perfSize->start($sqlSize);
127 145
 		$this->getResultSize($sqlSize);
146
+		$perfSize->stop();
147
+
148
+		$perfData->start($sqlData);
128 149
 		$a = $this->sendResult($sqlData);
150
+		$perfData->stop();
129 151
 
130
-		$this->perfTime += microtime(true)-$startTime;
152
+		$this->aPerf[] = $perfSize;
153
+		$this->aPerf[] = $perfData;
131 154
 
132 155
 		return $a;
133 156
 	} // function
134 157
 
135 158
 	function getNewer($id) {
159
+		$perf = new LggrPerf();
160
+
136 161
 		$sqlData = "
137 162
 SELECT * FROM LastHour
138 163
 WHERE id>$id
139 164
 ORDER BY `date` DESC
140 165
 LIMIT " . LggrState::PAGELEN;
141 166
 
142
-		return $this->sendResult($sqlData);
167
+		$perf->start($sqlData);
168
+		$a = $this->sendResult($sqlData);
169
+		$perf->stop();
170
+
171
+		$this->aPerf[] = $perf;
172
+
173
+		return $a;
143 174
 	} // function
144 175
 
145 176
 	function getFromTo($from=0, $count=LggrState::PAGELEN) {
146
-		$this->perfCount += 2;
147
-		$startTime = microtime(true);
177
+		$perfSize = new LggrPerf();
178
+		$perfData = new LggrPerf();
148 179
 
149 180
 		$sFrom = $this->db->escape_string($this->state->getFrom());
150 181
 		$sTo   = $this->db->escape_string($this->state->getTo());
@@ -159,17 +190,23 @@ WHERE `date` BETWEEN '$sFrom' AND '$sTo'
159 190
 ORDER BY `date` DESC
160 191
 LIMIT $from,$count";
161 192
 
193
+		$perfSize->start($sqlSize);
162 194
 		$this->getResultSize($sqlSize);
195
+		$perfSize->stop();
196
+
197
+		$perfData->start($sqlData);
163 198
 		$a = $this->sendResult($sqlData);
199
+		$perfData->stop();
164 200
 
165
-		$this->perfTime += microtime(true)-$startTime;
201
+		$this->aPerf[] = $perfSize;
202
+		$this->aPerf[] = $perfData;
166 203
 
167 204
 		return $a;
168 205
 	} // function
169 206
 
170 207
 	function getFiltered($host=null, $level=null, $from=0, $count=LggrState::PAGELEN) {
171
-		$this->perfCount += 2;
172
-		$startTime = microtime(true);
208
+		$perfSize = new LggrPerf();
209
+		$perfData = new LggrPerf();
173 210
 
174 211
 		$v = $this->getViewName();
175 212
 
@@ -193,17 +230,22 @@ LIMIT $from,$count";
193 230
 
194 231
 		$sqlData .= " ORDER BY `date` DESC LIMIT $from,$count";
195 232
 
233
+		$perfSize->start($sqlSize);
196 234
 		$this->getResultSize($sqlSize);
235
+		$perfSize->stop();
236
+
237
+		$perfData->start($sqlData);
197 238
 		$a = $this->sendResult($sqlData);
239
+		$perfData->stop();
198 240
 
199
-		$this->perfTime += microtime(true)-$startTime;
241
+		$this->aPerf[] = $perfSize;
242
+		$this->aPerf[] = $perfData;
200 243
 
201 244
 		return $a;
202 245
 	} // function
203 246
 
204 247
 	function getText($msg='', $prog='', $from=0, $count=LggrState::PAGELEN) {
205
-		$this->perfCount++;
206
-		$startTime = microtime(true);
248
+		$perf = new LggrPerf();
207 249
 
208 250
 		$v = $this->getViewName();
209 251
 		$sTmpMsg = $this->db->escape_string($msg);
@@ -224,42 +266,63 @@ WHERE $sWhere
224 266
 ORDER BY `date` DESC
225 267
 LIMIT $from,$count";
226 268
 
269
+		$perf->start($sql);
227 270
 		$a = $this->sendResult($sql);
271
+		$perf->stop();
228 272
 
229
-		$this->perfTime += microtime(true)-$startTime;
273
+		$this->aPerf[] = $perf;
230 274
 
231 275
 		return $a;
232 276
 	} // function
233 277
 
234 278
 	function getMessagesPerHour() {
235
-		$this->perfCount++;
236
-		$startTime = microtime(true);
279
+		$perf = new LggrPerf();
237 280
 
238 281
 		$sql = "
239 282
 SELECT HOUR(TIME(`date`)) AS h, COUNT(*) AS c
240 283
 FROM Today
241 284
 GROUP BY h";
242 285
 
286
+		$a = $this->cache->retrieve('mph');
287
+		if(null != $a) {
288
+			return $a;
289
+		} // if
290
+
291
+		$perf->start($sql);
243 292
 		$a = $this->sendResult($sql);
293
+		$perf->stop();
244 294
 
245
-		$this->perfTime += microtime(true)-$startTime;
295
+		$this->aPerf[] = $perf;
246 296
 
297
+		$this->cache->store('mph', $a);
247 298
 		return $a;
248 299
 	} // function
249 300
 
250 301
 	function getStatistic() {
302
+		$perf = new LggrPerf();
303
+
251 304
 		$sql = "
252 305
 SELECT COUNT(*) AS cnt, MIN(`date`) AS oldest
253 306
 FROM newlogs
254 307
 ";
255
-		return $this->sendResult($sql);
308
+
309
+		$a = $this->cache->retrieve('stats');
310
+		if(null != $a) {
311
+			return $a;
312
+		} // if
313
+
314
+		$perf->start($sql);
315
+		$a = $this->sendResult($sql);
316
+		$perf->stop();
317
+
318
+		$this->aPerf[] = $perf;
319
+
320
+		$this->cache->store('stats', $a);
321
+		return $a;
256 322
 	} // function
257 323
 
258 324
 	/* delete anything older than maxage hours, or 4 weeks */
259 325
 	function purgeOldMessages($maxage=672) {
260
-		$this->perfCount++;
261
-		$startTime = microtime(true);
262
-
263 326
 		$sql = "
264 327
 DELETE FROM newlogs
265 328
 WHERE `date` < (NOW() - INTERVAL $maxage hour)
@@ -269,11 +332,68 @@ WHERE `date` < (NOW() - INTERVAL $maxage hour)
269 332
 			throw new Exception($this->db->error);
270 333
 		} // if
271 334
 
272
-		$this->perfTime += microtime(true)-$startTime;
273
-
274 335
 		return $this->db->affected_rows;
275 336
 	} // function
276 337
 
338
+	function normalizeHosts() {
339
+
340
+		// Find any new hostnames
341
+		$sql = "
342
+SELECT newlogs.host
343
+FROM newlogs
344
+LEFT JOIN hosts ON hosts.name=newlogs.host
345
+WHERE hosts.id IS NULL
346
+GROUP BY newlogs.host";
347
+		$aEmpty = $this->sendResult($sql);
348
+		foreach($aEmpty as $o) {
349
+			$host = $o->host;
350
+			$host = $this->db->escape_string($host);
351
+
352
+			$sql = "INSERT INTO hosts (name) VALUES ('$host')";
353
+			$res = $this->db->query($sql);
354
+			if(false === $res) {
355
+				throw new Exception($this->db->error);
356
+			} // if
357
+			$id = $this->db->insert_id;
358
+			// echo "New $id for $host\n";
359
+
360
+			$sql = "UPDATE newlogs SET idhost=$id WHERE host='$host'";
361
+			$res = $this->db->query($sql);
362
+			if(false === $res) {
363
+				throw new Exception($this->db->error);
364
+			} // if
365
+		} // foreach
366
+
367
+		// read current list of hostnames and ids
368
+		$sql = "
369
+SELECT *
370
+FROM hosts";
371
+		$aTmp = $this->sendResult($sql);
372
+		$aHosts = array();
373
+		foreach($aTmp as $o) {
374
+			$hostId = $o->id;
375
+			$hostName = $o->name;
376
+			$aHosts[$hostName] = $hostId;
377
+		} // foreach
378
+
379
+		// search any new entry without hostid and update it
380
+		foreach($aHosts as $hostName => $hostId) {
381
+			// echo "updating $hostName to $hostId\n";
382
+			$hostName = $this->db->escape_string($hostName);
383
+			$sql = "
384
+UPDATE newlogs
385
+SET idhost=$hostId
386
+WHERE idhost IS NULL
387
+AND host='$hostName'
388
+";
389
+			$res = $this->db->query($sql);
390
+			if(false === $res) {
391
+				throw new Exception($this->db->error);
392
+			} // if
393
+		} // foreach
394
+
395
+	} // function
396
+
277 397
 
278 398
 	private function getResultSize($sql) {
279 399
 		$res = $this->db->query($sql);
@@ -303,10 +423,7 @@ WHERE `date` < (NOW() - INTERVAL $maxage hour)
303 423
 	} // function
304 424
 
305 425
 	public function getPerf() {
306
-		return array(
307
-			'count' => $this->perfCount,
308
-			'time' =>  $this->perfTime
309
-		);
426
+		return $this->aPerf;
310 427
 	} // function
311 428
 
312 429
 } // class

+ 51
- 0
inc/lggrcache_class.php View File

@@ -0,0 +1,51 @@
1
+<?php
2
+
3
+class LggrCache {
4
+	const MAXAGE = 300; // 5 minutes
5
+	private $cachepath=null;
6
+
7
+	function __construct() {
8
+		$this->cachepath = __DIR__ . '/../cache/';
9
+	} // constructor
10
+
11
+	public function store($key, $value) {
12
+		$fname = $this->getFilename($key);
13
+		$s = serialize($value);
14
+		file_put_contents($fname, $s);
15
+	} // function
16
+
17
+	public function retrieve($key) {
18
+		$fname = $this->getFilename($key);
19
+		if(file_exists($fname) && is_readable($fname)) {
20
+			$ts = filemtime($fname);
21
+			if(time() - $ts > self::MAXAGE) {
22
+				unlink($fname);
23
+				return null;
24
+			} else {
25
+				$s = file_get_contents($fname);
26
+				$a = unserialize($s);
27
+				return $a;
28
+			} // if
29
+		} else {
30
+			return null;
31
+		} // if
32
+	} // function
33
+
34
+	public function purge($key) {
35
+		$fname = $this->getFilename($key);
36
+		unlink($fname);
37
+	} // function
38
+
39
+
40
+	private function filterKey($key) {
41
+		$sTmp = str_replace(' ', '-', $key);
42
+		$sTmp = preg_replace('/[^A-Za-z0-9\-]/', '', $sTmp);
43
+		return $sTmp;
44
+	} // function
45
+
46
+	private function getFilename($key) {
47
+		return $this->cachepath . 'key_' . $this->filterKey($key) . '.data';
48
+	} // function
49
+
50
+}
51
+

+ 37
- 0
inc/lggrperf_class.php View File

@@ -0,0 +1,37 @@
1
+<?php
2
+
3
+class LggrPerf {
4
+	private $tsStart=null;
5
+	private $tsEnd=null;
6
+	private $tsLen=null;
7
+	private $sQuery=null;
8
+
9
+	function __construct() {
10
+	} // constructor
11
+
12
+	public function start($sql) {
13
+		$this->sQuery = $sql;
14
+		$this->tsStart = microtime(true);
15
+	} // function
16
+
17
+	public function stop() {
18
+		$this->tsEnd = microtime(true);
19
+		$this->tsLen = $this->tsEnd - $this->tsStart;
20
+	} // function
21
+
22
+	public function getPerf() {
23
+		$a = array();
24
+
25
+		$a['time'] = $this->tsLen;
26
+		$a['query'] = $this->sQuery;
27
+
28
+		$this->logperf();
29
+
30
+		return $a;
31
+	} // function
32
+
33
+	private function logPerf() {
34
+		error_log("Perf " . $this->tsLen . ", query " . $this->sQuery);
35
+	} // function
36
+}
37
+

+ 3
- 0
index.php View File

@@ -55,6 +55,7 @@ try {
55 55
 
56 56
 		$aEvents = $l->getFiltered($host, $level, $page*LggrState::PAGELEN, LggrState::PAGELEN);
57 57
 		$searchvalue='';
58
+		$searchvalueprog='';
58 59
 		$isSearch=false;
59 60
 		$sFilter='';
60 61
 		if($state->isHost())
@@ -67,6 +68,7 @@ try {
67 68
 		$aEvents = $l->getFromTo($page*LggrState::PAGELEN, LggrState::PAGELEN);
68 69
 		$sFilter = 'Filter by time range between <strong>' . htmlentities($state->getFrom()) . '</strong> and <strong>' . htmlentities($state->getTo()) . '</strong>';
69 70
 		$searchvalue='';
71
+		$searchvalueprog='';
70 72
 		$isSearch=false;
71 73
 
72 74
 	} else {
@@ -75,6 +77,7 @@ try {
75 77
 
76 78
 		$aEvents = $l->getLatest($page*LggrState::PAGELEN, LggrState::PAGELEN);
77 79
 		$searchvalue='';
80
+		$searchvalueprog='';
78 81
 		$isSearch=false;
79 82
 
80 83
 	} // if search

+ 10
- 1
tpl/foot.inc.php View File

@@ -1,7 +1,16 @@
1 1
     <div class="container">
2 2
       <hr>
3 3
       <footer>
4
-        <p class="debugfooter"><?= $aPerf['count'] ?> queries in <?= $aPerf['time'] ?> seconds. Session: <?= $_COOKIE['PHPSESSID'] ?> by <?= htmlentities($_SERVER['REMOTE_USER']) ?></p>
4
+<?php
5
+$pCount = count($aPerf);
6
+$pTime  = 0;
7
+foreach($aPerf as $perf) {
8
+	$aTmp = $perf->getPerf();
9
+
10
+	$pTime += $aTmp['time'];
11
+} // foreach
12
+?>
13
+        <p class="debugfooter"><?= $pCount ?> queries in <?= $pTime ?> seconds. Session: <?= $_COOKIE['PHPSESSID'] ?> by <?= htmlentities($_SERVER['REMOTE_USER']) ?></p>
5 14
         <p>&copy; <a href="http://lggr.io" target="_blank">lggr.io</a> 2015</p>
6 15
       </footer>
7 16
     </div> <!-- /container -->

Loading…
Cancel
Save
Social stuff:
Mastodon