1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Aptarus\PromClient\Exposition; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
use Aptarus\PromClient\Utility as U; |
7
|
|
|
use Aptarus\PromClient\Configuration as Configuration; |
8
|
|
|
|
9
|
|
|
class Emit |
10
|
|
|
{ |
11
|
|
|
public function __construct() |
12
|
|
|
{ |
13
|
|
|
$metrics_db = U\PromClientOpenDB(Configuration::$storage_dir); |
14
|
|
|
|
15
|
|
|
// Clean up the DB first. |
16
|
|
|
$julian_day = unixtojd(); |
17
|
|
|
$sth = $metrics_db-> |
18
|
|
|
prepare('DELETE FROM metrics |
19
|
|
|
WHERE var IN (SELECT var FROM meta |
20
|
|
|
WHERE ? - JULIANDAY(changed) > 3)'); |
21
|
|
|
$sth->execute(array($julian_day)); |
22
|
|
|
$meta_deleted = $sth->fetchColumn(); |
|
|
|
|
23
|
|
|
$sth = $metrics_db->prepare('DELETE FROM metrics |
24
|
|
|
WHERE ? - JULIANDAY(changed) > 3'); |
25
|
|
|
$sth->execute(array($julian_day)); |
26
|
|
|
$metics_deleted = $sth->fetchColumn(); |
|
|
|
|
27
|
|
|
|
28
|
|
|
$this->meta = $metrics_db |
|
|
|
|
29
|
|
|
->query('SELECT var, typ, help FROM meta ORDER BY var ASC') |
30
|
|
|
->fetchAll(PDO::FETCH_UNIQUE); |
31
|
|
|
|
32
|
|
|
$sth = $metrics_db |
33
|
|
|
->prepare('SELECT labels, value, label_values |
34
|
|
|
FROM metrics where var = ? ORDER BY var ASC'); |
35
|
|
|
|
36
|
|
|
foreach (array_keys($this->meta) as $var) { |
37
|
|
|
if ($sth->execute(array($var))) { |
38
|
|
|
$this->metrics[$var] = $sth->fetchAll(PDO::FETCH_UNIQUE); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// @codingStandardsIgnoreStart |
44
|
|
|
public function Text() |
45
|
|
|
{ |
46
|
|
|
// @codingStandardsIgnoreEnd |
47
|
|
|
// Spew metrics. |
48
|
|
|
header('Content-Type: text/plain; version=0.0.4'); |
49
|
|
|
|
50
|
|
|
foreach ($this->meta as $var) { |
51
|
|
|
print("# HELP $var " + $this->meta[$var]['help'] + "\n"); |
52
|
|
|
print("# TYPE $var " + $this->meta[$var]['typ'] + "\n"); |
53
|
|
|
foreach ($this->metrics[$var] as $labels) { |
54
|
|
|
$labels_quoted = ''; |
|
|
|
|
55
|
|
|
$lnames = unserialize($labels); |
56
|
|
|
if ($lnames) { |
57
|
|
|
$lvalues = unserialize( |
58
|
|
|
$this->metrics[$var][$labels]['label_values'] |
59
|
|
|
); |
60
|
|
|
$lnv = $array_combine($lnames, $lvalues); |
|
|
|
|
61
|
|
|
$lnv_quoted = array(); |
62
|
|
|
foreach ($lnv as $l) { |
63
|
|
|
$lnv_quoted[] = $l + '="' + $lvalues[$l] + '"'; |
64
|
|
|
} |
65
|
|
|
$labels_quoted = '{' + join(',', $lnv_quoted) + '}'; |
|
|
|
|
66
|
|
|
} |
67
|
|
|
print( |
68
|
|
|
"$var$plabels_quoted " |
|
|
|
|
69
|
|
|
+ $this->metrics[$var][$labels]['value'] + "\n" |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// vim:sw=4 ts=4 et |
77
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.