dashboard-monitoring.plugin.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <?php
  2. /*
  3. @name dash_monitoring
  4. @author Valentin CARRUESCO <idleman@idleman.fr>
  5. @link http://blog.idleman.fr
  6. @licence CC by nc sa
  7. @version 1.0.0
  8. @description Ajoute des widgets de monitoring du raspberry pi
  9. */
  10. function dash_monitoring_plugin_menu(&$widgets){
  11. $widgets[] = array(
  12. 'uid' => 'dash_monitoring_clock',
  13. 'icon' => 'fa fa-calendar-o',
  14. 'label' => 'Horloge',
  15. 'background' => '#394264',
  16. 'color' => '#fffffff',
  17. 'unique' => true,
  18. 'onLoad' => 'action.php?action=dash_monitoring_plugin_load&bloc=clock',
  19. 'onMove' => 'action.php?action=dash_monitoring_plugin_move&bloc=clock',
  20. 'onDelete' => 'action.php?action=dash_monitoring_plugin_delete&bloc=clock',
  21. );
  22. $widgets[] = array(
  23. 'uid' => 'dash_monitoring_ram',
  24. 'icon' => 'fa fa-bar-chart-o',
  25. 'label' => 'RAM',
  26. 'background' => '#50597B',
  27. 'color' => '#fffffff',
  28. 'onLoad' => 'action.php?action=dash_monitoring_plugin_load&bloc=ram',
  29. 'onMove' => 'action.php?action=dash_monitoring_plugin_move&bloc=ram',
  30. 'onDelete' => 'action.php?action=dash_monitoring_plugin_delete&bloc=ram',
  31. );
  32. $widgets[] = array(
  33. 'uid' => 'dash_monitoring_vocal',
  34. 'icon' => 'fa fa-comments-o',
  35. 'label' => 'Commandes vocales',
  36. 'background' => '#014B96',
  37. 'color' => '#fffffff',
  38. 'onLoad' => 'action.php?action=dash_monitoring_plugin_load&bloc=vocal',
  39. 'onMove' => 'action.php?action=dash_monitoring_plugin_move&bloc=vocal',
  40. 'onDelete' => 'action.php?action=dash_monitoring_plugin_delete&bloc=vocal',
  41. );
  42. $widgets[] = array(
  43. 'uid' => 'dash_monitoring_logs',
  44. 'icon' => 'fa fa-dedent',
  45. 'label' => 'Logs',
  46. 'background' => '#0FC7E3',
  47. 'color' => '#fffffff',
  48. 'onLoad' => 'action.php?action=dash_monitoring_plugin_load&bloc=logs'
  49. );
  50. $widgets[] = array(
  51. 'uid' => 'dash_monitoring_system',
  52. 'icon' => 'fa fa-tachometer',
  53. 'label' => 'Système',
  54. 'background' => '#84C400',
  55. 'color' => '#fffffff',
  56. 'onLoad' => 'action.php?action=dash_monitoring_plugin_load&bloc=system',
  57. 'onMove' => 'action.php?action=dash_monitoring_plugin_move&bloc=system',
  58. 'onDelete' => 'action.php?action=dash_monitoring_plugin_delete&bloc=system',
  59. );
  60. $widgets[] = array(
  61. 'uid' => 'dash_monitoring_network',
  62. 'icon' => 'fa fa-exchange',
  63. 'label' => 'Réseau',
  64. 'background' => '#006AFF',
  65. 'color' => '#fffffff',
  66. 'onLoad' => 'action.php?action=dash_monitoring_plugin_load&bloc=network',
  67. 'onMove' => 'action.php?action=dash_monitoring_plugin_move&bloc=network',
  68. 'onDelete' => 'action.php?action=dash_monitoring_plugin_delete&bloc=network',
  69. );
  70. $widgets[] = array(
  71. 'uid' => 'dash_monitoring_hdd',
  72. 'icon' => 'fa fa-hdd-o',
  73. 'label' => 'HDD',
  74. 'background' => '#FF2E12',
  75. 'color' => '#fffffff',
  76. 'onLoad' => 'action.php?action=dash_monitoring_plugin_load&bloc=hdd',
  77. 'onMove' => 'action.php?action=dash_monitoring_plugin_move&bloc=hdd',
  78. 'onDelete' => 'action.php?action=dash_monitoring_plugin_delete&bloc=hdd',
  79. );
  80. $widgets[] = array(
  81. 'uid' => 'dash_monitoring_disk',
  82. 'icon' => 'fa fa-download',
  83. 'label' => 'Disques',
  84. 'background' => '#C1004F',
  85. 'color' => '#fffffff',
  86. 'onLoad' => 'action.php?action=dash_monitoring_plugin_load&bloc=disk',
  87. 'onMove' => 'action.php?action=dash_monitoring_plugin_move&bloc=disk',
  88. 'onDelete' => 'action.php?action=dash_monitoring_plugin_delete&bloc=disk',
  89. );
  90. $widgets[] = array(
  91. 'uid' => 'dash_monitoring_users',
  92. 'icon' => 'fa fa-users',
  93. 'label' => 'Utilisateurs',
  94. 'background' => '#E51400',
  95. 'color' => '#fffffff',
  96. 'onLoad' => 'action.php?action=dash_monitoring_plugin_load&bloc=users',
  97. 'onMove' => 'action.php?action=dash_monitoring_plugin_move&bloc=users',
  98. 'onDelete' => 'action.php?action=dash_monitoring_plugin_delete&bloc=users',
  99. );
  100. $widgets[] = array(
  101. 'uid' => 'dash_monitoring_services',
  102. 'icon' => 'fa fa-users',
  103. 'label' => 'Services',
  104. 'background' => '#632F00',
  105. 'color' => '#fffffff',
  106. 'onLoad' => 'action.php?action=dash_monitoring_plugin_load&bloc=services',
  107. 'onMove' => 'action.php?action=dash_monitoring_plugin_move&bloc=services',
  108. 'onDelete' => 'action.php?action=dash_monitoring_plugin_delete&bloc=services',
  109. );
  110. $widgets[] = array(
  111. 'uid' => 'dash_monitoring_gpio',
  112. 'icon' => 'fa fa-dot-circle-o',
  113. 'label' => 'GPIO',
  114. 'background' => '#373737',
  115. 'color' => '#fffffff',
  116. 'onLoad' => 'action.php?action=dash_monitoring_plugin_load&bloc=gpio',
  117. 'onMove' => 'action.php?action=dash_monitoring_plugin_move&bloc=gpio',
  118. 'onDelete' => 'action.php?action=dash_monitoring_plugin_delete&bloc=gpio',
  119. );
  120. }
  121. function dash_monitoring_plugin_actions(){
  122. global $myUser,$_,$conf;
  123. switch($_['action']){
  124. case 'dash_monitoring_plugin_load':
  125. if($myUser==false) exit('Vous devez vous connecter pour cette action.');
  126. header('Content-type: application/json');
  127. $response = array();
  128. switch($_['bloc']){
  129. case 'ram':
  130. $response['title'] = 'RAM';
  131. $hdds = Monitoring::ram();
  132. $response['content'] = '
  133. <div style="width: 100%">
  134. <canvas id="RAM_PIE"></canvas>
  135. <br/><br/>
  136. <ul class="graphic_pane">
  137. <li class="pane_orange">
  138. <h1>RAM UTILISEE</h1>
  139. <h2>'.$hdds['percentage'].'%</h2>
  140. </li><li class="pane_cyan">
  141. <h1>RAM LIBRE</h1>
  142. <h2>'.$hdds['free'].' Mo</h2>
  143. </li><li class="pane_red">
  144. <h1>RAM TOTALE</h1>
  145. <h2>'.$hdds['total'].' Mo</h2>
  146. </li>
  147. </ul>
  148. </div>
  149. <script>
  150. $("#RAM_PIE:visible").chart({
  151. type : "doughnut",
  152. label : ["RAM UTILISEE","RAM LIBRE"],
  153. backgroundColor : ["'.($hdds['percentage']>80? '#E64C65' : '#FCB150' ).'","#4FC4F6"],
  154. segmentShowStroke:false,
  155. data : ['.$hdds['percentage'].','.(100-$hdds['percentage']).']
  156. });
  157. </script>';
  158. break;
  159. case 'clock':
  160. $response['title'] = 'Horloge';
  161. $response['content'] = '
  162. <style>
  163. .clockContainer{
  164. background-color:#394264;
  165. font-family:\'Open Sans Light\';
  166. width:100%;
  167. height:100%;
  168. box-sizing:border-box;
  169. text-align:center;
  170. padding:20px;
  171. }
  172. .clock {
  173. height: 200px;
  174. width: 200px;
  175. margin:auto;
  176. position:relative;
  177. background-color:#394264;
  178. }
  179. .progress > svg {
  180. height: 100%;
  181. display: block;
  182. }
  183. .progressbar-text{
  184. position:absolute;
  185. left:0px;
  186. top:60px;
  187. width:100%;
  188. text-align:center;
  189. font-weight:200;
  190. color:#ffffff!important;
  191. }
  192. .progressbar-text .dayName,.progressbar-text .dayDate{
  193. font-size:20px;
  194. color:#cecece;
  195. margin: 10px 0;
  196. }
  197. .progressbar-text .dayName{
  198. font-weight:bold;
  199. color:#ffffff;
  200. margin-top: 20px ;
  201. }
  202. </style>
  203. <div class="clockContainer">
  204. <div class="clock" id="clock"></div>
  205. </div>
  206. <script>
  207. var circle;
  208. $(document).ready(function(){
  209. circle = new ProgressBar.Circle(\'#clock\', {
  210. color: \'#50C8FB\',
  211. duration: 3000,
  212. easing: \'easeInOut\',
  213. text: {
  214. style : { fontSize : \'38px\'},
  215. value : \'00:00:00\'
  216. }
  217. });
  218. refresh_clock();
  219. setInterval(function(){
  220. refresh_clock();
  221. },1000);
  222. });
  223. function refresh_clock(){
  224. var d = new Date();
  225. var hour = d.getHours();
  226. var minut = ("00" + d.getMinutes()).slice(-2) ;
  227. var second = ("00" + d.getSeconds()).slice(-2) ;
  228. var year = d.getFullYear() ;
  229. var month = ("00" + (d.getMonth()+1)).slice(-2) ;
  230. var day = ("00" + d.getDate()).slice(-2) ;
  231. var days = ["Dimanche","Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi"];
  232. var dayName = days[d.getDay()];
  233. circle.setText(hour+\':\'+minut+\':\'+second+\'<div class="dayName">\'+dayName+\'</div><div class="dayDate">\'+day+\'/\'+month+\'/\'+year+\'</div>\');
  234. circle.set(second/60);
  235. }
  236. </script>';
  237. break;
  238. case 'system':
  239. $response['title'] = 'Système';
  240. if(PHP_OS!='WINNT'){
  241. $heat = Monitoring::heat();
  242. $cpu = Monitoring::cpu();
  243. }
  244. $response['content'] = '<ul class="yana-list">
  245. <li><strong>Distribution :</strong> '.Monitoring::distribution().'</li>
  246. <li><strong>Kernel :</strong> '.Monitoring::kernel().'</li>
  247. <li><strong>HostName :</strong> '.Monitoring::hostname().'</li>
  248. <li><strong>Température :</strong> <span class="label '.$heat["label"].'">'.$heat["degrees"].'°C</span></li>
  249. <li><strong>Temps de marche :</strong> '.Monitoring::uptime().'</li>
  250. <li><strong>CPU :</strong> <span class="label label-info">'.$cpu['current_frequency'].' Mhz</span> (Max '.$cpu['maximum_frequency'].' Mhz/ Min '.$cpu['minimum_frequency'].' Mhz)</li>
  251. <li><strong>Charge :</strong> <span class="label label-info">'.$cpu['loads'].' </span> | '.$cpu['loads5'].' 5min | '.$cpu['loads15'].' 15min</li>
  252. </ul>';
  253. break;
  254. case 'vocal':
  255. if($myUser->getId()=='') exit('{"error":"invalid or missing token"}');
  256. if(!$myUser->can('vocal','r')) exit('{"error":"insufficient permissions for this account"}');
  257. $protocol = 'http'.(isset($_SERVER['HTTPS'])?"s":"");
  258. list($host,$port) = explode(':',$_SERVER['HTTP_HOST']);
  259. $actionUrl = $protocol.'://'.$host.':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
  260. $actionUrl = substr($actionUrl,0,strpos($actionUrl , '?'));
  261. Plugin::callHook("vocal_command", array(&$response,$actionUrl));
  262. $response['title'] = count($response['commands']).' Commandes vocales';
  263. $response['content'] = '<ul class="yana-list">';
  264. if(isset($response['commands'])){
  265. foreach($response['commands'] as $command){
  266. $response['content'] .= '<li title="Sensibilité : '.$command['confidence'].'" onclick="window.open(\''.$command['url'].'\')">'.$command['command'].'</li>';
  267. }
  268. }
  269. $response['content'] .= '</ul>';
  270. break;
  271. case 'logs':
  272. if($myUser->getId()=='') exit('{"error":"invalid or missing token"}');
  273. $response['title'] = 'Logs';
  274. $logs = dirname(__FILE__).'/../../'.LOG_FILE ;
  275. $response['content'] = '<div style="overflow:auto;max-height:200px;"><ul class="yana-list" style="margin:0px;">';
  276. if(file_exists($logs)){
  277. $lines = file($logs);
  278. $nb_lines = count($lines)-1; // -1 pour ne pas avoir la ligne avec uniquement la virgule
  279. for($i=$nb_lines; $i>0; $i--){
  280. $response['content'] .= '<li style="font-size:8px;'.($i%2==0?'background-color:#F4F4F4;':'').'">'.$line.'</li>';
  281. }
  282. }else{
  283. $response['content'] .= '<li>Aucun logs</li>';
  284. }
  285. $response['content'] .= '</ul></div>';
  286. break;
  287. case 'network':
  288. $response['title'] = 'Réseau';
  289. $ethernet = array();
  290. $lan = '';
  291. $wan = '';
  292. $http = '';
  293. $connections = '';
  294. if(PHP_OS!='WINNT'){
  295. $ethernet = Monitoring::ethernet();
  296. $lan = Monitoring::internalIp();
  297. $wan = Monitoring::externalIp();
  298. $http = Monitoring::webServer();
  299. $connections = Monitoring::connections();
  300. }
  301. $response['content'] = '<ul class="yana-list">
  302. <li><strong>IP LAN :</strong> <code>'.$lan.'</code></li>
  303. <li><strong>IP WAN :</strong> <code>'.$wan.'</code></li>
  304. <li><strong>Serveur HTTP :</strong> '.$http.'</li>
  305. <li><strong>Ethernet :</strong> '.$ethernet['up'].' Montant / '.$ethernet['down'].' Descendant</li>
  306. <li><strong>Connexions :</strong> <span class="label label-info">'.$connections.'</span></li>
  307. </ul>';
  308. break;
  309. case 'gpio':
  310. $response['title'] = 'GPIO';
  311. $gpios = array();
  312. $gpios = Monitoring::gpio();
  313. $model = System::getModel();
  314. $pinLabelsRange = System::getPinForModel($model['type'],$model['version']);
  315. $response['title'] = 'RPI Type '.$model['type'].' V'.$model['version'].' ('.count($gpios).' pins)';
  316. $response['content'] .= '<table class="gpio_container">';
  317. $response['content'] .= '<tr>';
  318. foreach($pinLabelsRange as $range=>$pinLabels){
  319. $response['content'] .= '<td valign="top"><table>';
  320. foreach($pinLabels as $pin){
  321. $roleColor = 'transparent';
  322. $class = 'gpio_state_'.($gpios[$pin->wiringPiNumber]?'on':'off');
  323. if($pin->name=='5V' || $pin->name=='3.3V') $class = 'gpio_power';
  324. if($pin->name=='0V' || $pin->name=='DNC') $class = 'gpio_ground';
  325. $response['content'] .= '<div class="'.$class.'" title="Role : '.($pin->role==''?'GPIO':$pin->role).', Position physique : '.$pin->physicalNumber.', Numero BMC : '.$pin->bcmNumber.'" onclick="change_gpio_state('.$pin->wiringPiNumber.',this);">';
  326. $response['content'] .= '<span style="'.($range==0?'float:right;':'').'"></span> '.$pin->name.' <small>('.($pin->role==''?'GPIO':$pin->role).')</small>';
  327. $response['content'] .= '</div>';
  328. }
  329. $response['content'] .= '</table></td>';
  330. }
  331. $response['content'] .= '</tr>';
  332. $response['content'] .= '</table>';
  333. if($model['type']=='unknown'){
  334. $response['title'] = 'RPI Inconnu, révision ('.$model['revision'].') ';
  335. $response['content'] = '<p>Votre RPI dispose d\'une révision ('.$model['revision'].') inconnue de yana, il n\'est pas possible d\'afficher ses pins.<br/> Pour ajouter cette révision, merci de <a href="https://github.com/ldleman/yana-server/issues">faire une demande sur github</a> en précisant la révision, le nombre de pins, le modèle et la vesion de votre rpi.</p>';
  336. }
  337. break;
  338. case 'users':
  339. $users = array();
  340. if(PHP_OS!='WINNT'){
  341. $users = Monitoring::users();
  342. }
  343. $response['title'] = count($users).' utilisateurs connectés';
  344. $response['content'] = '<ul class="yana-list">';
  345. foreach ($users as $value) {
  346. $response['content'] .= '<li>Utilisateur <strong class="badge">'.$value['user'].'</strong> IP : <code>'.$value['ip'].'</code>, Connexion : '.$value['hour'].' </li>';
  347. }
  348. $response['content'] .= '</ul>';
  349. break;
  350. case 'services':
  351. $response['title'] = 'Services';
  352. $services = array();
  353. if(PHP_OS!='WINNT'){
  354. $services = Monitoring::services();
  355. $response['content'] = '<ul class="yana-list">';
  356. foreach ($services as $value) {
  357. $response['content'] .= '<li '.($value['status']?'class="service-active"':'').'>- '.$value['name'].'</li>';
  358. }
  359. $response['content'] .= '</ul>';
  360. }else{
  361. $response['content'] .='Information indisponible sur cet OS :'.PHP_OS;
  362. }
  363. break;
  364. case 'hdd':
  365. $response['title'] = 'HDD';
  366. $hdds = array();
  367. if(PHP_OS!='WINNT'){
  368. $hdds = Monitoring::hdd();
  369. $response['content'] ='<ul class="yana-list">';
  370. foreach ($hdds as $value) {
  371. $response['content'] .='<li><strong class="badge">'.$value['name'].'</strong><br><strong> Espace :</strong> '.$value['used'].'/'.$value['total'].'<strong> Format :</strong> '.$value['format'].' </li>';
  372. }
  373. $response['content'] .='</ul>';
  374. }else{
  375. $response['content'] .='Information indisponible sur cet OS :'.PHP_OS;
  376. }
  377. break;
  378. case 'disk':
  379. $response['title'] = 'Disques';
  380. $disks = array();
  381. if(PHP_OS!='WINNT'){
  382. $disks = Monitoring::disks();
  383. $response['content'] ='<ul class="yana-list">';
  384. foreach ($disks as $value) {
  385. $response['content'] .='<li><strong class="badge">'.$value['name'].'</strong> Statut : '.$value['size'].' Type : '.$value['type'].' Chemin : '.$value['mountpoint'].' </li>';
  386. }
  387. $response['content'] .='</ul>';
  388. }else{
  389. $response['content'] .='Information indisponible sur cet OS :'.PHP_OS;
  390. }
  391. break;
  392. }
  393. echo json_encode($response);
  394. exit(0);
  395. break;
  396. case 'dash_monitoring_plugin_edit':
  397. echo '<label>Time Zone</label><input id="zone" type="text">';
  398. break;
  399. case 'dash_monitoring_plugin_save':
  400. break;
  401. case 'dash_monitoring_plugin_delete':
  402. break;
  403. case 'dash_monitoring_plugin_move':
  404. break;
  405. }
  406. }
  407. Plugin::addCss('/css/style.css',true);
  408. Plugin::addJs('/js/main.js',true);
  409. Plugin::addHook("widgets", "dash_monitoring_plugin_menu");
  410. Plugin::addHook("action_post_case", "dash_monitoring_plugin_actions");
  411. ?>