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
|
<?php
namespace Icinga\Module\Director\Clicommands;
use Icinga\Application\Benchmark;
use Icinga\Data\Filter\Filter;
use Icinga\Data\Filter\FilterChain;
use Icinga\Data\Filter\FilterExpression;
use Icinga\Module\Director\Cli\Command;
use Icinga\Module\Director\CustomVariable\CustomVariable;
use Icinga\Module\Director\Data\Db\IcingaObjectFilterRenderer;
use Icinga\Module\Director\Data\Db\IcingaObjectQuery;
use Icinga\Module\Director\Objects\HostGroupMembershipResolver;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Objects\IcingaHostVar;
use Icinga\Module\Director\Objects\IcingaVar;
class BenchmarkCommand extends Command
{
public function testflatfilterAction()
{
$q = new IcingaObjectQuery('host', $this->db());
$filter = Filter::fromQueryString(
// 'host.vars.snmp_community="*ub*"&(host.vars.location="London"|host.vars.location="Berlin")'
// 'host.vars.snmp_community="*ub*"&(host.vars.location="FRA DC"|host.vars.location="NBG DC")'
'host.vars.priority="*igh"&(host.vars.location="FRA DC"|host.vars.location="NBG DC")'
);
IcingaObjectFilterRenderer::apply($filter, $q);
echo $q->getSql() . "\n";
print_r($q->listNames());
}
public function rerendervarsAction()
{
$conn = $this->db();
$db = $conn->getDbAdapter();
$db->beginTransaction();
$query = $db->select()->from(
array('v' => 'icinga_var'),
array(
'v.varname',
'v.varvalue',
'v.checksum',
'v.rendered_checksum',
'v.rendered',
'format' => "('json')",
)
);
Benchmark::measure('Ready to fetch all vars');
$rows = $db->fetchAll($query);
Benchmark::measure('Got vars, storing flat');
foreach ($rows as $row) {
$var = CustomVariable::fromDbRow($row);
$rendered = $var->render();
$checksum = sha1($rendered, true);
if ($checksum === $row->rendered_checksum) {
continue;
}
$where = $db->quoteInto('checksum = ?', $row->checksum);
$db->update(
'icinga_var',
array(
'rendered' => $rendered,
'rendered_checksum' => $checksum
),
$where
);
}
$db->commit();
}
public function flattenvarsAction()
{
$conn = $this->db();
$db = $conn->getDbAdapter();
$db->beginTransaction();
$query = $db->select()->from(['v' => 'icinga_host_var'], [
'v.host_id',
'v.varname',
'v.varvalue',
'v.format',
'v.checksum'
]);
Benchmark::measure('Ready to fetch all vars');
$rows = $db->fetchAll($query);
Benchmark::measure('Got vars, storing flat');
foreach ($rows as $row) {
$var = CustomVariable::fromDbRow($row);
$checksum = $var->checksum();
if (! IcingaVar::exists($checksum, $conn)) {
IcingaVar::generateForCustomVar($var, $conn);
}
if ($row->checksum === null) {
$where = $db->quoteInto('host_id = ?', $row->host_id)
. $db->quoteInto(' AND varname = ?', $row->varname);
$db->update('icinga_host_var', ['checksum' => $checksum], $where);
}
}
$db->commit();
}
public function resolvehostgroupsAction()
{
$resolver = new HostGroupMembershipResolver($this->db());
$resolver->refreshDb();
}
public function filterAction()
{
$flat = [];
/** @var FilterChain|FilterExpression $filter */
$filter = Filter::fromQueryString(
// 'object_name=*ic*2*&object_type=object'
'vars.bpconfig=*'
);
Benchmark::measure('ready');
$objs = IcingaHost::loadAll($this->db());
Benchmark::measure('db done');
foreach ($objs as $host) {
$flat[$host->get('id')] = (object) [];
foreach ($host->getProperties() as $k => $v) {
$flat[$host->get('id')]->$k = $v;
}
}
Benchmark::measure('objects ready');
$vars = IcingaHostVar::loadAll($this->db());
Benchmark::measure('vars loaded');
foreach ($vars as $var) {
if (! array_key_exists($var->get('host_id'), $flat)) {
// Templates?
continue;
}
$flat[$var->get('host_id')]->{'vars.' . $var->get('varname')} = $var->get('varvalue');
}
Benchmark::measure('vars done');
foreach ($flat as $host) {
if ($filter->matches($host)) {
echo $host->object_name . "\n";
}
}
}
}
|