diff options
author | M.Gergő | 2020-04-07 00:07:24 +0200 |
---|---|---|
committer | M.Gergő | 2020-04-07 00:07:24 +0200 |
commit | f90344c9dc1bac3ea4d4f6ebeb3c81025700227e (patch) | |
tree | 88b5dd046d8eec807a83bf82515516e7bdc1b699 /nextcloud_munin.php | |
parent | 97138c37d19aac798fe772c02523d4c34cef365a (diff) | |
download | munin-nextcloud-f90344c9dc1bac3ea4d4f6ebeb3c81025700227e.tar.gz munin-nextcloud-f90344c9dc1bac3ea4d4f6ebeb3c81025700227e.zip |
Munin graphs for Nextcloud
Diffstat (limited to 'nextcloud_munin.php')
-rw-r--r-- | nextcloud_munin.php | 372 |
1 files changed, 156 insertions, 216 deletions
diff --git a/nextcloud_munin.php b/nextcloud_munin.php index f18468b..8675235 100644 --- a/nextcloud_munin.php +++ b/nextcloud_munin.php @@ -1,11 +1,8 @@ - - #!/usr/bin/php <?php - $set['host'] = getenv('host'); -//$set['occ_user'] = getenv('occ_user'); +//$set['occ_user'] = getenv('occ_user'); //If "serverinfo" would be a part of occ commands... //$set['occ_path'] = getenv('occ_path'); $set['db_user'] = getenv('db_user'); $set['db_pass'] = getenv('db_pass'); @@ -13,8 +10,9 @@ $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['nxt_plugins'] = explode(" ", getenv('nxt_plugins')); +/* //For debugging +$set['host'] = "cloud.example.com"; $set['db_user'] = "root"; $set['db_pass'] = ""; $set['db_db'] = "Nextcloud_db"; @@ -22,264 +20,206 @@ $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(); - +$replace_chars = array("\"","\$","@","^","`",",","|","%",";",".","~","(",")","/","\\","{","}",":","?","[","]","=","+","#","!","-",); // Special chars to replace +$time_req = time()-60*$set['diff_minutes']; + +//--------------------------------------------------------------------------------------------------------------------------------------// + function prepare_auth_stat($l){ + global $set, $time_req, $replace_chars; + $ret = array(); + $ret['g_name'] = "nxt_act_users_".str_replace($replace_chars, "_", $set['host'] ); + $ret['g_host'] = $set['host']; + $ret['g_title'] = "Nextcloud Active Users"; + $ret['g_vlabel'] = "Users"; + $ret['g_category'] = "Nxt_actv_users"; + $ret['g_info'] = "nextcloud_active_users"; + $ret['g_order'] = "nxt_users_all nxt_users_web nxt_users_app"; + + // skeleton for returning + foreach(array('users_all', 'users_web', 'users_app', 'users_win', 'users_linux', 'users_oos') as $key => $val){ + $ret['data'][$val]['value'] = 0; + $ret['data'][$val]['draw'] = "LINE1.2"; + $ret['data'][$val]['type'] = "GAUGE"; + } + $ret['data']['users_all']['label'] = "Total"; + $ret['data']['users_all']['info'] = "Total clients"; + $ret['data']['users_web']['label'] = "By Web"; + $ret['data']['users_web']['info'] = "Clients logged in by webpage."; + $ret['data']['users_app']['label'] = "By App"; + $ret['data']['users_app']['info'] = "Connected clients with an app."; + $ret['data']['users_win']['label'] = "Windows"; + $ret['data']['users_win']['info'] = "Clients on windows"; + $ret['data']['users_linux']['label'] = "Linux"; + $ret['data']['users_linux']['info'] = "Clients on Linux"; + $ret['data']['users_oos']['label'] = "Other OS"; + $ret['data']['users_oos']['info'] = "Clients on other OS"; - 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 "); - + $r = mysqli_query($l, "SELECT * FROM ".mysqli_real_escape($l, $set['db_db']).".".mysqli_real_escape($l, $set['db_prefix'])."authtoken + WHERE `last_activity` >= ".mysqli_real_escape($l, $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'] ++; + if($row['type'] == 0) { //with own password (usually on web browser) + if(strpos($row['name'], "mirall" ) !== false){ //with own password on (mirall) desktop client + $ret['data']['users_app']['value']++; + } else { //password over web + $ret['data']['users_web']['value']++; } - } else if($row['type'] == 1) { //with application - $ret['app'] ++; + } else if($row['type'] == 1) { //with token --> (usually on app) + $ret['data']['users_app']['value']++; } - $ret['all'] ++; // everybody + $ret['data']['users_all']['value']++; //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'], 'windows') !== false){ //If "windows" in User Agent + $ret['data']['users_win']['value']++; + } else if(stripos($row['name'], 'linux') !== false){ //If "Linux" in User Agent + $ret['data']['users_linux']['value']++; + } else { //Other OS + $ret['data']['users_oos']['value']++; } - - 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; + return $ret; } +//--------------------------------------------------------------------------------------------------------------------------------------// + function prepare_event_stat($l){ + global $set, $time_req, $replace_chars; + $d_pfx = "evt_"; + $ret['g_name'] = "nxt_events_".str_replace($replace_chars, "_", $set['host'] ); + $ret['g_host'] = $set['host']; + $ret['g_title'] = "Nextcloud Activity"; + $ret['g_vlabel'] = "Events"; + $ret['g_category'] = "Nxt_events"; + $ret['g_info'] = "nextcloud_events"; + $ret['g_order'] = "nxt_evt_all nxt_evt_files nxt_evt_share"; + // skeleton for returning + foreach(array_merge(array('all', 'files', 'share', 'pub', 'comm', 'cal', 'other'), $set['nxt_plugins']) as $key => $val){ + $ret['data'][$d_pfx.$val]['value'] = 0; + $ret['data'][$d_pfx.$val]['draw'] = "LINE1.2"; + $ret['data'][$d_pfx.$val]['type'] = "GAUGE"; + } + $ret['data'][$d_pfx.'all']['label'] = "Total events"; + $ret['data'][$d_pfx.'all']['info'] = "Total Actions generated"; + $ret['data'][$d_pfx.'files']['label'] = "File actions"; + $ret['data'][$d_pfx.'files']['info'] = "Files operations"; + $ret['data'][$d_pfx.'share']['label'] = "Saring actions"; + $ret['data'][$d_pfx.'share']['info'] = "Sharing by groups or users inside"; + $ret['data'][$d_pfx.'pub']['label'] = "Pub.link download"; + $ret['data'][$d_pfx.'pub']['info'] = "Public Link downloaded"; + $ret['data'][$d_pfx.'comm']['label'] = "Comments"; + $ret['data'][$d_pfx.'comm']['info'] = "Comments"; + $ret['data'][$d_pfx.'cal']['label'] = "Calendar actions"; + $ret['data'][$d_pfx.'cal']['info'] = "Calendar actions/operations"; + $ret['data'][$d_pfx.'other']['label'] = "Other actions"; + $ret['data'][$d_pfx.'other']['info'] = "Other actions"; + //------------ Applications enabled ------------/ + $ret['data'][$d_pfx.'talk']['label'] = "Talk events"; + $ret['data'][$d_pfx.'talk']['info'] = "Nextcloud Talk events"; + $ret['data'][$d_pfx.'antivirus']['label'] = "Virus detect"; + $ret['data'][$d_pfx.'antivirus']['info'] = "Virus Detections by files_antivirus"; - 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)) { + mysqli_set_charset($l, "utf8"); + $r = mysqli_query($l, " SELECT * FROM ".mysqli_real_escape_string($l, $set['db_db']).".".mysqli_real_escape_string($l, $set['db_prefix'])."activity + WHERE `timestamp` >= ".mysqli_real_escape_string($l, $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 ;" + ); + 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'] ++; - } + $ret['data'][$d_pfx.'files']['value']++; + + } else if(stripos($row['type'], 'shared') !== false ){ + $ret['data'][$d_pfx.'share']['value']++; + + } else if(stripos($row['type'], 'public_link') !== false ){ + $ret['data'][$d_pfx.'pub']['value']++; + + } else if(stripos($row['type'], 'comments') !== false ){ + $ret['data'][$d_pfx.'comm']['value']++; + + } else if(stripos($row['type'], 'calendar') !== false ){ + $ret['data'][$d_pfx.'cal']['value']++; + + } else if(stripos($row['type'], 'spreed') !== false ){ //nextcloud_talk app + $ret['data'][$d_pfx.'talk']['value']++; + + } else if(stripos($row['type'], 'virus_detected') !== false ){ // files_antivirus app + $ret['data'][$d_pfx.'antivirus']['value']++; + } else { + $ret['data'][$d_pfx.'other']['value']++; + } + $ret['data'][$d_pfx.'all']['value']++; + } + @mysqli_free_result($r); + return $ret; + } - 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){ + function print_config($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 .= "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).".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; + //echo iconv("UTF-8", "ISO-8859-2", $cf).PHP_EOL; //If you want Hungarian chars + echo $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 = "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; + //echo iconv("UTF-8", "ISO-8859-2", $cf).PHP_EOL; //If you want Hungarian chars + echo $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 + echo "\n MYSQL error. \n"; } else { - $auth = get_auth_stat($l); - $actv = get_activity_stat($l); + $auth_stat = prepare_auth_stat($l); + $actv_stat = prepare_event_stat($l); + if (isset($argv[1]) and $argv[1] == "config"){ + print_config($auth_stat); + print_config($actv_stat); + } else { + print_values($auth_stat); + print_values($actv_stat); + } @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); -} ?> |