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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
#!/usr/bin/php
<?php
$set['host'] = getenv('host');
//$set['occ_user'] = getenv('occ_user');
//$set['occ_path'] = getenv('occ_path');
$set['db_user'] = getenv('db_user');
$set['db_pass'] = getenv('db_pass');
$set['db_db'] = getenv('db_db');
$set['db_host'] = getenv('db_host');
$set['db_prefix'] = getenv('db_prefix');
$set['diff_minutes'] = intval(getenv('diff_minutes'));
/*
$set['host'] = "felho.example.com";
$set['db_user'] = "root";
$set['db_pass'] = "";
$set['db_db'] = "Nextcloud_db";
$set['db_host'] = "localhost";
$set['db_prefix'] = "oc_";
$set['diff_minutes'] = 5; // --> 5 minutes
*/
$replace_chars = array("\"","\$","@","^","`",",","|","%",";",".","~","(",")","/","\\","{","}",":","?","[","]","=","+","#","!","-",); // Special chars replace
$time_req = time() - 60*$set['diff_minutes'];
$dta = array();
$dtv = array();
function get_auth_stat($l) {
global $set, $time_req;
$ret = array('all' => 0,'app' => 0, 'web' => 0, 'windows' => 0, 'linux' => 0, 'other_os' => 0, 'firefox' => 0, 'chrome' => 0, 'safari' => 0, 'other_bw' => 0 );
mysqli_set_charset($l, "utf8");
$r = mysqli_query($l," SELECT * FROM ".$set['db_db'].".".$set['db_prefix']."authtoken WHERE `last_activity` >= ".$time_req." ORDER BY `last_activity` DESC ");
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
if($row['type'] == 0) { //web login
if(strpos($row['name'], "mirall" ) !== false){ //without token on desktop
$ret['app'] ++;
} else {
$ret['web'] ++;
}
} else if($row['type'] == 1) { //with application
$ret['app'] ++;
}
$ret['all'] ++; // everybody
if(stripos($row['name'], 'windows') !== false){
$ret['windows']++;
} else if(stripos($row['name'], 'linux') !== false){
$ret['linux']++;
} else {
$ret['other_os']++;
}
if(stripos($row['name'], 'firefox') !== false){
$ret['firefox']++;
} else if(stripos($row['name'], 'chrome') !== false){ //Chrome is using Safari motor also
$ret['chrome']++;
} else if(stripos($row['name'], 'safari') !== false){
$ret['safari']++;
} else {
$ret['other_bw']++;
}
}
@mysqli_free_result($r);
return $ret;
}
function get_activity_stat($l) {
global $set, $time_req;
$ret = array('all' => 0, 'file' =>0, 'share' => 0, 'calendar' => 0, 'comment' => 0, 'public' => 0, 'settings' => 0, 'virus' => 0, 'talk' => 0);
mysqli_set_charset($l, "utf8");
$q = " SELECT * FROM ".$set['db_db'].".".$set['db_prefix']."activity
WHERE `timestamp` >= ".$time_req."
AND ( ((`type` LIKE '%file%' AND `subject` NOT LIKE '%by%')
OR (`type` LIKE '%shared%' AND `subject` NOT LIKE '%self%') )
OR ( `type` NOT LIKE '%file%' AND `type` NOT LIKE '%shared%') )
ORDER BY `timestamp` DESC ;
";
$r = mysqli_query($l,$q);
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
if(stripos($row['type'], 'file_') !== false ){
$ret['file'] ++;
}
if(stripos($row['type'], 'shared') !== false ){
$ret['share'] ++;
}
if(stripos($row['type'], 'calendar') !== false ){
$ret['calendar'] ++;
}
if(stripos($row['type'], 'virus_detected') !== false ){
if(!isset($ret['virus'])){ $ret['virus'] = 0; }
$ret['virus'] ++;
}
if(stripos($row['type'], 'comments') !== false ){
$ret['comment'] ++;
}
if(stripos($row['type'], 'public_link') !== false ){
$ret['public'] ++;
}
if(stripos($row['type'], 'settings') !== false ){
$ret['settings'] ++;
}
if(stripos($row['type'], 'spreed') !== false ){ //Nextcloud talk
if(!isset($ret['talk'])){$ret['talk'] = 0; }
$ret['talk'] ++;
}
$ret['all']++;
}
@mysqli_free_result($r);
return $ret;
}
function print_headers($inp){
global $replace_chars;
$cf = "multigraph ".$inp['g_name']."\n";
$cf .= "host_name ".$inp['g_host']."\n";
$cf .= "graph_title ".$inp['g_title']."\n";
$cf .= "graph_args --base 1000 \n";
$cf .= "graph_vlabel ".$inp['g_vlabel']."\n";
$cf .= "graph_category ".$inp['g_category']."\n";
$cf .= "graph_info ".$inp['g_info']."\n";
$cf .= "graph_order ".$inp['g_order']."\n";
foreach($inp['data'] as $key => $val){
$cf .= "nxt_".str_replace($replace_chars, "_", $key).".label ".$val['label']." \n";
$cf .= "nxt_".str_replace($replace_chars, "_", $key).".draw ".$val['draw']." \n";
$cf .= "nxt_".str_replace($replace_chars, "_", $key).".info ".$val['info']." \n";
$cf .= "nxt_".str_replace($replace_chars, "_", $key).".type ".$val['type']." \n";
}
$cf .= "\n";
echo iconv("UTF-8", "ISO-8859-2", $cf).PHP_EOL;
}
function print_values($inp){
global $replace_chars;
if(isset($inp['data'])){
$cf = "multigraph ".$inp['g_name']."\n";
foreach($inp['data'] as $key => $val){
$cf .= "nxt_".str_replace($replace_chars, "_", $key).".value ".$val['value']."\n";
}
$cf .= "\n";
echo iconv("UTF-8", "ISO-8859-2", $cf).PHP_EOL;
}
}
if (function_exists('mysqli_connect') and PHP_MAJOR_VERSION >= 7 ) { //mysqli && php > 7.0 needed
$l = mysqli_connect($set['db_host'], $set['db_user'], $set['db_pass'], $set['db_db']);
if(!$l){
//error
} else {
$auth = get_auth_stat($l);
$actv = get_activity_stat($l);
@mysqli_close($l);
}
}
$dta['g_name'] = "nxt_active_users_".str_replace($replace_chars, "_", $set['host'] );
$dta['g_host'] = $set['host'];
$dta['g_title'] = "Nextcloud Users";
$dta['g_vlabel'] = "Users";
$dta['g_category'] = "Nxt_users";
$dta['g_info'] = "nextcloud_active_users";
$dta['g_order'] = "nxt_users_all nxt_users_web nxt_users_app";
$dta['data']['users_all']['value'] = $auth['all'];
$dta['data']['users_all']['label'] = "Total";
$dta['data']['users_all']['draw'] = "LINE1.2";
$dta['data']['users_all']['info'] = "Total client";
$dta['data']['users_all']['type'] = "GAUGE";
$dta['data']['users_web']['value'] = $auth['web'];
$dta['data']['users_web']['label'] = "By Web";
$dta['data']['users_web']['draw'] = "LINE1.2";
$dta['data']['users_web']['info'] = "Webpage clients";
$dta['data']['users_web']['type'] = "GAUGE";
$dta['data']['users_app']['value'] = $auth['app'];
$dta['data']['users_app']['label'] = "By APP";
$dta['data']['users_app']['draw'] = "LINE1.2";
$dta['data']['users_app']['info'] = "Connected clients by app.";
$dta['data']['users_app']['type'] = "GAUGE";
$dta['data']['users_win']['value'] = $auth['windows'];
$dta['data']['users_win']['label'] = "Windows";
$dta['data']['users_win']['draw'] = "LINE1.2";
$dta['data']['users_win']['info'] = "Total client";
$dta['data']['users_win']['type'] = "GAUGE";
$dta['data']['users_linux']['value'] = $auth['linux'];
$dta['data']['users_linux']['label'] = "Linux";
$dta['data']['users_linux']['draw'] = "LINE1.2";
$dta['data']['users_linux']['info'] = "Webpage clients";
$dta['data']['users_linux']['type'] = "GAUGE";
$dta['data']['users_oos']['value'] = $auth['other_os'];
$dta['data']['users_oos']['label'] = "Other OS";
$dta['data']['users_oos']['draw'] = "LINE1.2";
$dta['data']['users_oos']['info'] = "Connected clients by app.";
$dta['data']['users_oos']['type'] = "GAUGE";
$dtb['g_name'] = "nxt_events_".str_replace($replace_chars, "_", $set['host'] );
$dtb['g_host'] = $set['host'];
$dtb['g_title'] = "Nextcloud Users Activity";
$dtb['g_vlabel'] = "Events";
$dtb['g_category'] = "Nxt_events";
$dtb['g_info'] = "nextcloud_events";
$dtb['g_order'] = "nxt_act_all nxt_act_files nxt_act_share";
$dtb['data']['act_all']['value'] = $actv['all'];
$dtb['data']['act_all']['label'] = "Total events";
$dtb['data']['act_all']['draw'] = "LINE1.2";
$dtb['data']['act_all']['info'] = "Total client";
$dtb['data']['act_all']['type'] = "GAUGE";
$dtb['data']['act_files']['value'] = $actv['file'];
$dtb['data']['act_files']['label'] = "File operations";
$dtb['data']['act_files']['draw'] = "LINE1.2";
$dtb['data']['act_files']['info'] = "Webpage clients";
$dtb['data']['act_files']['type'] = "GAUGE";
$dtb['data']['act_share']['value'] = $actv['share'];
$dtb['data']['act_share']['label'] = "Saring events";
$dtb['data']['act_share']['draw'] = "LINE1.2";
$dtb['data']['act_share']['info'] = "Sharing to groups or users";
$dtb['data']['act_share']['type'] = "GAUGE";
$dtb['data']['act_pub']['value'] = $actv['public'];
$dtb['data']['act_pub']['label'] = "Pub.link download";
$dtb['data']['act_pub']['draw'] = "LINE1.2";
$dtb['data']['act_pub']['info'] = "Sharing by public links";
$dtb['data']['act_pub']['type'] = "GAUGE";
$dtb['data']['act_comm']['value'] = $actv['comment'];
$dtb['data']['act_comm']['label'] = "Comments";
$dtb['data']['act_comm']['draw'] = "LINE1.2";
$dtb['data']['act_comm']['info'] = "Comments";
$dtb['data']['act_comm']['type'] = "GAUGE";
$dtb['data']['act_talk']['value'] = $actv['talk'];
$dtb['data']['act_talk']['label'] = "Talk events";
$dtb['data']['act_talk']['draw'] = "LINE1.2";
$dtb['data']['act_talk']['info'] = "settings Change";
$dtb['data']['act_talk']['type'] = "GAUGE";
$dtb['data']['act_cal']['value'] = $actv['calendar'];
$dtb['data']['act_cal']['label'] = "Calendar operations";
$dtb['data']['act_cal']['draw'] = "LINE1.2";
$dtb['data']['act_cal']['info'] = "Calendar.";
$dtb['data']['act_cal']['type'] = "GAUGE";
$dtb['data']['act_virus']['value'] = $actv['virus'];
$dtb['data']['act_virus']['label'] = "Virus detection";
$dtb['data']['act_virus']['draw'] = "LINE1.2";
$dtb['data']['act_virus']['info'] = "Virus Detected";
$dtb['data']['act_virus']['type'] = "GAUGE";
$dtb['data']['act_setts']['value'] = $actv['settings'];
$dtb['data']['act_setts']['label'] = "Change Settings";
$dtb['data']['act_setts']['draw'] = "LINE1.2";
$dtb['data']['act_setts']['info'] = "settings Change";
$dtb['data']['act_setts']['type'] = "GAUGE";
if (isset($argv[1]) and $argv[1] == "config"){
print_headers($dta);
print_headers($dtb);
} else {
print_values($dta);
print_values($dtb);
}
?>
|