netdata-functions.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. function netdataSettngsArray()
  3. {
  4. $array = array(
  5. 'name' => 'Netdata',
  6. 'enabled' => true,
  7. 'image' => 'plugins/images/tabs/netdata.png',
  8. 'category' => 'Monitor',
  9. 'settings' => array(
  10. 'Enable' => array(
  11. array(
  12. 'type' => 'switch',
  13. 'name' => 'homepageNetdataEnabled',
  14. 'label' => 'Enable',
  15. 'value' => $GLOBALS['homepageNetdataEnabled']
  16. ),
  17. array(
  18. 'type' => 'select',
  19. 'name' => 'homepageNetdataAuth',
  20. 'label' => 'Minimum Authentication',
  21. 'value' => $GLOBALS['homepageNetdataAuth'],
  22. 'options' => groupSelect()
  23. )
  24. ),
  25. 'Connection' => array(
  26. array(
  27. 'type' => 'html',
  28. 'override' => 12,
  29. 'label' => 'Info',
  30. 'html' => 'The URL needs to be on the same domain as your Organizr, and be proxied by subdomain. E.g. If Organizr is accessed at: https://domain.com, then your URL for netdata should be: https://netdata.domain.com'
  31. ),
  32. array(
  33. 'type' => 'input',
  34. 'name' => 'netdataURL',
  35. 'label' => 'URL',
  36. 'value' => $GLOBALS['netdataURL'],
  37. 'help' => 'Please enter the local IP:PORT of your netdata instance'
  38. ),
  39. array(
  40. 'type' => 'blank',
  41. 'label' => ''
  42. ),
  43. ),
  44. )
  45. );
  46. for($i = 1; $i <= 7; $i++) {
  47. $array['settings']['Chart '.$i] = array(
  48. array(
  49. 'type' => 'switch',
  50. 'name' => 'netdata'.$i.'Enabled',
  51. 'label' => 'Enable',
  52. 'value' => $GLOBALS['netdata'.$i.'Enabled']
  53. ),
  54. array(
  55. 'type' => 'blank',
  56. 'label' => ''
  57. ),
  58. array(
  59. 'type' => 'input',
  60. 'name' => 'netdata'.$i.'Title',
  61. 'label' => 'Title',
  62. 'value' => $GLOBALS['netdata'.$i.'Title'],
  63. 'help' => 'Title for the netdata graph'
  64. ),
  65. array(
  66. 'type' => 'select',
  67. 'name' => 'netdata'.$i.'Data',
  68. 'label' => 'Data',
  69. 'value' => $GLOBALS['netdata'.$i.'Data'],
  70. 'options' => netdataOptions(),
  71. ),
  72. array(
  73. 'type' => 'select',
  74. 'name' => 'netdata'.$i.'Chart',
  75. 'label' => 'Chart',
  76. 'value' => $GLOBALS['netdata'.$i.'Chart'],
  77. 'options' => netdataChartOptions(),
  78. ),
  79. array(
  80. 'type' => 'select',
  81. 'name' => 'netdata'.$i.'Colour',
  82. 'label' => 'Colour',
  83. 'value' => $GLOBALS['netdata'.$i.'Colour'],
  84. 'options' => netdataColourOptions(),
  85. ),
  86. array(
  87. 'type' => 'select',
  88. 'name' => 'netdata'.$i.'Size',
  89. 'label' => 'Size',
  90. 'value' => $GLOBALS['netdata'.$i.'Size'],
  91. 'options' => netdataSizeOptions(),
  92. ),
  93. array(
  94. 'type' => 'blank',
  95. 'label' => ''
  96. ),
  97. array(
  98. 'type' => 'switch',
  99. 'name' => 'netdata'.$i.'lg',
  100. 'label' => 'Show on large screens',
  101. 'value' => $GLOBALS['netdata'.$i.'lg']
  102. ),
  103. array(
  104. 'type' => 'switch',
  105. 'name' => 'netdata'.$i.'md',
  106. 'label' => 'Show on medium screens',
  107. 'value' => $GLOBALS['netdata'.$i.'md']
  108. ),
  109. array(
  110. 'type' => 'switch',
  111. 'name' => 'netdata'.$i.'sm',
  112. 'label' => 'Show on small screens',
  113. 'value' => $GLOBALS['netdata'.$i.'sm']
  114. ),
  115. );
  116. }
  117. $array['settings']['Options'] = array(
  118. array(
  119. 'type' => 'select',
  120. 'name' => 'homepageNetdataRefresh',
  121. 'label' => 'Refresh Seconds',
  122. 'value' => $GLOBALS['homepageNetdataRefresh'],
  123. 'options' => optionTime()
  124. ),
  125. );
  126. return $array;
  127. }
  128. function disk($dimension, $url)
  129. {
  130. $data = [];
  131. // Get Data
  132. $dataUrl = $url . '/api/v1/data?chart=system.io&dimensions='.$dimension.'&format=array&points=540&group=average&gtime=0&options=absolute|jsonwrap|nonzero&after=-540';
  133. try {
  134. $response = Requests::get($dataUrl);
  135. if ($response->success) {
  136. $json = json_decode($response->body, true);
  137. $data['value'] = $json['latest_values'][0] / 1000;
  138. $data['percent'] = getPercent($json['latest_values'][0], $json['max']);
  139. $data['units'] = 'MiB/s';
  140. $data['max'] = $json['max'];
  141. }
  142. } catch (Requests_Exception $e) {
  143. writeLog('error', 'Netdata Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  144. };
  145. return $data;
  146. }
  147. function diskSpace($dimension, $url)
  148. {
  149. $data = [];
  150. // Get Data
  151. $dataUrl = $url . '/api/v1/data?chart=disk_space._&format=json&points=509&group=average&gtime=0&options=ms|jsonwrap|nonzero&after=-540&dimension='.$dimension;
  152. try {
  153. $response = Requests::get($dataUrl);
  154. if ($response->success) {
  155. $json = json_decode($response->body, true);
  156. $data['value'] = $json['result']['data'][0][1];
  157. $data['percent'] = $data['value'];
  158. $data['units'] = '%';
  159. $data['max'] = 100;
  160. }
  161. } catch (Requests_Exception $e) {
  162. writeLog('error', 'Netdata Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  163. };
  164. return $data;
  165. }
  166. function net($dimension, $url)
  167. {
  168. $data = [];
  169. // Get Data
  170. $dataUrl = $url . '/api/v1/data?chart=system.net&dimensions='.$dimension.'&format=array&points=540&group=average&gtime=0&options=absolute|jsonwrap|nonzero&after=-540';
  171. try {
  172. $response = Requests::get($dataUrl);
  173. if ($response->success) {
  174. $json = json_decode($response->body, true);
  175. $data['value'] = $json['latest_values'][0] / 1000;
  176. $data['percent'] = getPercent($json['latest_values'][0], $json['max']);
  177. $data['units'] = 'Mbit/s';
  178. $data['max'] = $json['max'];
  179. }
  180. } catch (Requests_Exception $e) {
  181. writeLog('error', 'Netdata Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  182. };
  183. return $data;
  184. }
  185. function cpu($url)
  186. {
  187. $data = [];
  188. $dataUrl = $url . '/api/v1/data?chart=system.cpu&format=array';
  189. try {
  190. $response = Requests::get($dataUrl);
  191. if ($response->success) {
  192. $json = json_decode($response->body, true);
  193. $data['value'] = $json[0];
  194. $data['percent'] = $data['value'];
  195. $data['max'] = 100;
  196. $data['units'] = '%';
  197. }
  198. } catch (Requests_Exception $e) {
  199. writeLog('error', 'Netdata Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  200. };
  201. return $data;
  202. }
  203. function ram($url)
  204. {
  205. $data = [];
  206. $dataUrl = $url . '/api/v1/data?chart=system.ram&format=array&points=540&group=average&gtime=0&options=absolute|percentage|jsonwrap|nonzero&after=-540&dimensions=used|buffers|active|wired';
  207. try {
  208. $response = Requests::get($dataUrl);
  209. if ($response->success) {
  210. $json = json_decode($response->body, true);
  211. $data['value'] = $json['result'][0];
  212. $data['percent'] = $data['value'];
  213. $data['max'] = 100;
  214. $data['units'] = '%';
  215. }
  216. } catch (Requests_Exception $e) {
  217. writeLog('error', 'Netdata Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  218. };
  219. return $data;
  220. }
  221. function swap($url)
  222. {
  223. $data = [];
  224. $dataUrl = $url . '/api/v1/data?chart=system.swap&format=array&points=540&group=average&gtime=0&options=absolute|percentage|jsonwrap|nonzero&after=-540&dimensions=used';
  225. try {
  226. $response = Requests::get($dataUrl);
  227. if ($response->success) {
  228. $json = json_decode($response->body, true);
  229. $data['value'] = $json['result'][0];
  230. $data['percent'] = $data['value'];
  231. $data['max'] = 100;
  232. $data['units'] = '%';
  233. }
  234. } catch (Requests_Exception $e) {
  235. writeLog('error', 'Netdata Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  236. };
  237. return $data;
  238. }
  239. function ipmiTemp($url, $unit)
  240. {
  241. $data = [];
  242. $dataUrl = $url . '/api/v1/data?chart=ipmi.temperatures_c&format=array&points=540&group=average&gtime=0&options=absolute|jsonwrap|nonzero&after=-540';
  243. try {
  244. $response = Requests::get($dataUrl);
  245. if ($response->success) {
  246. $json = json_decode($response->body, true);
  247. $data['value'] = $json['result'][0];
  248. if($unit == 'c') {
  249. $data['percent'] = ($data['value'] / 50) * 100;
  250. $data['max'] = 50;
  251. } else if($unit == 'f') {
  252. $data['value'] = ($data['value'] * 9/5) + 32;
  253. $data['percent'] = ($data['value'] / 122) * 100;
  254. $data['max'] = 122;
  255. }
  256. $data['units'] = '°'.strtoupper($unit);
  257. }
  258. } catch (Requests_Exception $e) {
  259. writeLog('error', 'Netdata Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  260. };
  261. return $data;
  262. }
  263. function getPercent($val, $max)
  264. {
  265. if($max == 0) {
  266. return 0;
  267. } else {
  268. return ( $val / $max ) * 100;
  269. }
  270. }
  271. function cpuTemp($url, $unit)
  272. {
  273. $data = [];
  274. $dataUrl = $url . '/api/v1/data?chart=sensors.coretemp-isa-0000_temperature&format=json&points=509&group=average&gtime=0&options=ms|flip|jsonwrap|nonzero&after=-540';
  275. try {
  276. $response = Requests::get($dataUrl);
  277. if ($response->success) {
  278. $json = json_decode($response->body, true);
  279. $vals = $json['latest_values'];
  280. $vals = array_filter($vals);
  281. if(count($vals) > 0) {
  282. $data['value'] = array_sum($vals) / count($vals);
  283. } else {
  284. $data['value'] = 0;
  285. }
  286. if($unit == 'c') {
  287. $data['percent'] = ($data['value'] / 50) * 100;
  288. $data['max'] = 50;
  289. } else if($unit == 'f') {
  290. $data['value'] = ($data['value'] * 9/5) + 32;
  291. $data['percent'] = ($data['value'] / 122) * 100;
  292. $data['max'] = 122;
  293. }
  294. $data['units'] = '°'.strtoupper($unit);
  295. }
  296. } catch (Requests_Exception $e) {
  297. writeLog('error', 'Netdata Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  298. };
  299. return $data;
  300. }