-
Notifications
You must be signed in to change notification settings - Fork 0
/
ilias_exporter.php
181 lines (152 loc) · 5.95 KB
/
ilias_exporter.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
<?php
require __DIR__ . '/vendor/autoload.php';
use Prometheus\CollectorRegistry;
use Prometheus\RenderTextFormat;
class IliasExporter
{
private $con = null;
private $ildbuser;
private $ildbpassword;
private $ildb;
public function __construct()
{
$this->ildbuser = getenv('ildbuser') ? getenv('ildbuser') : 'root';
$this->ildbpassword = getenv('ildbpassword') ? getenv('ildbpassword') : '';
$this->ildb = getenv('ildb') ? getenv('ildb') : 'ilias';
}
private function connectdb()
{
$this->con = new mysqli('localhost', $this->ildbuser, $this->ildbpassword, $this->ildb);
if ($this->con->connect_error) {
die('Connect Error (' . $this->con->connect_errno . ') ' . $this->con->connect_error);
}
}
private function execute_sessions($registry)
{
$result = $this->con->query("select count(distinct user_id) from usr_session where `expires` > UNIX_TIMESTAMP( NOW( ) ) AND user_id != 0");
$usrs = $result->fetch_row()[0];
$gauge = $registry->getOrRegisterGauge('ilias', 'session', 'ILIAS Session', [
'ilsessions'
]);
$gauge->set($usrs, [
'ilsessions'
]);
}
private function execute_10minavg($registry)
{
$result = $this->con->query("select count(distinct user_id) from usr_session where 10 * 60 > UNIX_TIMESTAMP( NOW( ) ) - ctime AND user_id != 0");
$usrs = $result->fetch_row()[0];
$gauge = $registry->getOrRegisterGauge('ilias', '10minavg', 'ILIAS 10 avg', [
'il10minavg'
]);
$gauge->set($usrs, [
'il10minavg'
]);
}
private function execute_60minavg($registry)
{
$result = $this->con->query("select count(distinct user_id) from usr_session where 60 * 60 > UNIX_TIMESTAMP( NOW( ) ) - ctime AND user_id != 0");
$usrs = $result->fetch_row()[0];
$gauge = $registry->getOrRegisterGauge('ilias', '60minavg', 'ILIAS 60 avg', [
'il60minavg'
]);
$gauge->set($usrs, [
'il60minavg'
]);
}
private function execute_total1day($registry)
{
$result = $this->con->query("select count(usr_id) FROM `usr_data` WHERE last_login >= DATE_SUB( NOW( ) , INTERVAL 1 DAY )");
$usrs = $result->fetch_row()[0];
$gauge = $registry->getOrRegisterGauge('ilias', 'total1day', 'Users in 24h', [
'iltotal1day'
]);
$gauge->set($usrs, [
'iltotal1day'
]);
}
private function execute_total7days($registry)
{
$result = $this->con->query("select count(usr_id) FROM `usr_data` WHERE last_login >= DATE_SUB( NOW( ) , INTERVAL 7 DAY )");
$usrs = $result->fetch_row()[0];
$gauge = $registry->getOrRegisterGauge('ilias', 'total7days', 'Users in last 7 days', [
'iltotal7days'
]);
$gauge->set($usrs, [
'iltotal7days'
]);
}
private function execute_total90days($registry)
{
$result = $this->con->query("select count(usr_id) FROM `usr_data` WHERE last_login >= DATE_SUB( NOW( ) , INTERVAL 90 DAY )");
$usrs = $result->fetch_row()[0];
$gauge = $registry->getOrRegisterGauge('ilias', 'total90days', 'Users in 90 Days', [
'iltotal90days'
]);
$gauge->set($usrs, [
'iltotal90days'
]);
}
private function execute_objecttypeusage($registry)
{
$sql = "SELECT type, COUNT(obj_id) FROM `object_data` GROUP BY type";
$result = $this->con->query($sql);
$gauge = $registry->getOrRegisterGauge('ilias', 'objecttypeusage', 'Object type usage', [
'ObjectType'
]);
while ($questiontype = $result->fetch_row()) {
$gauge->set($questiontype[1], [
$questiontype[0]
]);
}
}
private function execute_questiontypeusage($registry)
{
$sql = "SELECT qpl_qst_type.type_tag , COUNT(tst_test_question.test_question_id) FROM `tst_test_question` LEFT JOIN `qpl_questions` ON tst_test_question.question_fi = qpl_questions.question_id RIGHT JOIN qpl_qst_type ON qpl_questions.question_type_fi = qpl_qst_type.question_type_id GROUP BY qpl_qst_type.question_type_id";
$result = $this->con->query($sql);
$gauge = $registry->getOrRegisterGauge('ilias', 'questiontypeusage', 'Question type usage', [
'QuestionType'
]);
while ($questiontype = $result->fetch_row()) {
$gauge->set($questiontype[1], [
$questiontype[0]
]);
}
}
private function execute_ecseventscount($registry)
{
$sql = "SELECT COUNT(id) FROM `ecs_events`";
$result = $this->con->query($sql);
$events = $result->fetch_row()[0];
$gauge = $registry->getOrRegisterGauge('ilias', 'ecseventscount', 'ECS Events Count', [
'ilecsevents'
]);
$gauge->set($events, [
'ilecsevents'
]);
}
public function run()
{
$adapter = new Prometheus\Storage\InMemory();
$registry = new CollectorRegistry($adapter);
$this->connectdb();
$this->execute_sessions($registry);
$this->execute_10minavg($registry);
$this->execute_60minavg($registry);
$this->execute_total1day($registry);
$this->execute_total7days($registry);
$this->execute_total90days($registry);
$this->execute_objecttypeusage($registry);
$this->execute_questiontypeusage($registry);
$this->execute_ecseventscount($registry);
if ($this->con) {
$this->con->close();
}
$renderer = new RenderTextFormat();
$result = $renderer->render($registry->getMetricFamilySamples());
header('Content-type: ' . RenderTextFormat::MIME_TYPE);
echo $result;
}
}
$exporter = new IliasExporter();
$exporter->run();