netdata.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. <?php
  2. trait NetDataHomepageItem
  3. {
  4. public function getNetdataHomepageData()
  5. {
  6. if (!$this->homepageItemPermissions($this->netdataHomepagePermissions('main'), true)) {
  7. return false;
  8. }
  9. $api = [];
  10. $api['data'] = [];
  11. $api['url'] = $this->config['netdataURL'];
  12. $url = $this->qualifyURL($this->config['netdataURL']);
  13. for ($i = 1; $i < 8; $i++) {
  14. if ($this->config['netdata' . ($i) . 'Enabled']) {
  15. switch ($this->config['netdata' . $i . 'Data']) {
  16. case 'disk-read':
  17. $data = $this->disk('in', $url);
  18. break;
  19. case 'disk-write':
  20. $data = $this->disk('out', $url);
  21. $data['value'] = (isset($data['value'])) ? abs($data['value']) : 0;
  22. $data['percent'] = (isset($data['percent'])) ? abs($data['percent']) : 0;
  23. break;
  24. case 'cpu':
  25. $data = $this->cpu($url);
  26. break;
  27. case 'net-in':
  28. $data = $this->net('received', $url);
  29. break;
  30. case 'net-out':
  31. $data = $this->net('sent', $url);
  32. $data['value'] = (isset($data['value'])) ? abs($data['value']) : 0;
  33. $data['percent'] = (isset($data['percent'])) ? abs($data['percent']) : 0;
  34. break;
  35. case 'ram-used':
  36. $data = $this->ram($url);
  37. break;
  38. case 'swap-used':
  39. $data = $this->swap($url);
  40. break;
  41. case 'disk-avail':
  42. $data = $this->diskSpace('avail', $url);
  43. break;
  44. case 'disk-used':
  45. $data = $this->diskSpace('used', $url);
  46. break;
  47. case 'ipmi-temp-c':
  48. $data = $this->ipmiTemp($url, 'c');
  49. break;
  50. case 'ipmi-temp-f':
  51. $data = $this->ipmiTemp($url, 'f');
  52. break;
  53. case 'cpu-temp-c':
  54. $data = $this->cpuTemp($url, 'c');
  55. break;
  56. case 'cpu-temp-f':
  57. $data = $this->cpuTemp($url, 'f');
  58. break;
  59. case 'custom':
  60. $data = $this->customNetdata($url, $i);
  61. break;
  62. default:
  63. $data = [
  64. 'title' => 'DNC',
  65. 'value' => 0,
  66. 'units' => 'N/A',
  67. 'max' => 100,
  68. ];
  69. break;
  70. }
  71. $data['title'] = $this->config['netdata' . $i . 'Title'];
  72. $data['colour'] = $this->config['netdata' . $i . 'Colour'];
  73. $data['chart'] = $this->config['netdata' . $i . 'Chart'];
  74. $data['size'] = $this->config['netdata' . $i . 'Size'];
  75. $data['lg'] = $this->config['netdata' . ($i) . 'lg'];
  76. $data['md'] = $this->config['netdata' . ($i) . 'md'];
  77. $data['sm'] = $this->config['netdata' . ($i) . 'sm'];
  78. array_push($api['data'], $data);
  79. }
  80. }
  81. $api = $api ?? false;
  82. $this->setAPIResponse('success', null, 200, $api);
  83. return $api;
  84. }
  85. public function netdataSettingsArray($infoOnly = false)
  86. {
  87. $homepageInformation = [
  88. 'name' => 'Netdata',
  89. 'enabled' => true,
  90. 'image' => 'plugins/images/tabs/netdata.png',
  91. 'category' => 'Monitor',
  92. 'settingsArray' => __FUNCTION__
  93. ];
  94. if ($infoOnly) {
  95. return $homepageInformation;
  96. }
  97. $homepageSettings = [
  98. 'debug' => true,
  99. 'settings' => [
  100. 'Enable' => [
  101. $this->settingsOption('enable', 'homepageNetdataEnabled'),
  102. $this->settingsOption('auth', 'homepageNetdataAuth'),
  103. ],
  104. 'Connection' => [
  105. $this->settingsOption('url', 'netdataURL'),
  106. $this->settingsOption('blank'),
  107. $this->settingsOption('disable-cert-check', 'netdataDisableCertCheck'),
  108. $this->settingsOption('use-custom-certificate', 'netdataUseCustomCertificate'),
  109. ],
  110. ]
  111. ];
  112. for ($i = 1; $i <= 7; $i++) {
  113. $homepageSettings['settings']['Chart ' . $i] = array(
  114. $this->settingsOption('enable', 'netdata' . $i . 'Enabled'),
  115. $this->settingsOption('blank'),
  116. $this->settingsOption('input', 'netdata' . $i . 'Title', ['label' => 'Title', 'help' => 'Title for the netdata graph']),
  117. $this->settingsOption('select', 'netdata' . $i . 'Data', ['label' => 'Data', 'options' => $this->netdataOptions()]),
  118. $this->settingsOption('select', 'netdata' . $i . 'Chart', ['label' => 'Chart', 'options' => $this->netdataChartOptions()]),
  119. $this->settingsOption('select', 'netdata' . $i . 'Colour', ['label' => 'Colour', 'options' => $this->netdataColourOptions()]),
  120. $this->settingsOption('select', 'netdata' . $i . 'Size', ['label' => 'Size', 'options' => $this->netdataSizeOptions()]),
  121. $this->settingsOption('blank'),
  122. $this->settingsOption('switch', 'netdata' . $i . 'lg', ['label' => 'Show on large screens']),
  123. $this->settingsOption('switch', 'netdata' . $i . 'md', ['label' => 'Show on medium screens']),
  124. $this->settingsOption('switch', 'netdata' . $i . 'sm', ['label' => 'Show on small screens']),
  125. );
  126. }
  127. $homepageSettings['settings']['Custom data'] = array(
  128. $this->settingsOption('html', null, ['label' => '', 'override' => 12, 'html' => '
  129. <div>
  130. <p>This is where you can define custom data sources for your netdata charts. To use a custom source, you need to select "Custom" in the data field for the chart.</p>
  131. <p>To define a custom data source, you need to add an entry to the JSON below, where the key is the chart number you want the custom data to be used for. Here is an example to set chart 1 custom data source to RAM percentage:</p>
  132. <pre>
  133. {
  134. "1": {
  135. "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",
  136. "value": "result,0",
  137. "units": "%",
  138. "max": 100
  139. }
  140. }
  141. </pre>
  142. <p>The URL is appended to your netdata URL and returns JSON formatted data. The value field tells Organizr how to return the value you want from the netdata API. This should be formatted as comma-separated keys to access the desired value.</p>
  143. <table class="table table-striped">
  144. <thead>
  145. <tr>
  146. <th>Parameter</th>
  147. <th>Description</th>
  148. <th>Required</th>
  149. </tr>
  150. </thead>
  151. <tbody>
  152. <tr>
  153. <td>url</td>
  154. <td>Specifies the netdata API endpoint</td>
  155. <td><i class="fa fa-check text-success" aria-hidden="true"></i></td>
  156. </tr>
  157. <tr>
  158. <td>value</td>
  159. <td>Specifies the selector used to get the data form the netdata response</td>
  160. <td><i class="fa fa-check text-success" aria-hidden="true"></i></td>
  161. </tr>
  162. <tr>
  163. <td>units</td>
  164. <td>Specifies the units shown in the graph/chart. Defaults to %</td>
  165. <td><i class="fa fa-times text-danger" aria-hidden="true"></i></td>
  166. </tr>
  167. <tr>
  168. <td>max</td>
  169. <td>Specifies the maximum possible value for the data. Defaults to 100</td>
  170. <td><i class="fa fa-times text-danger" aria-hidden="true"></i></td>
  171. </tr>
  172. <tr>
  173. <td>mutator</td>
  174. <td>Used to perform simple mathematical operations on the result (+, -, /, *). For example: dividing the result by 1000 would be "/1000". These operations can be chained together by putting them in a comma-seprated format.</td>
  175. <td><i class="fa fa-times text-danger" aria-hidden="true"></i></td>
  176. </tr>
  177. <tr>
  178. <td>netdata</td>
  179. <td>Can be used to override the netdata instance data is retrieved from (in the format: http://IP:PORT)</td>
  180. <td><i class="fa fa-times text-danger" aria-hidden="true"></i></td>
  181. </tr>
  182. </tbody>
  183. </table>
  184. </div>']),
  185. $this->settingsOption('html', 'netdataCustomTextAce', ['class' => 'jsonTextarea hidden', 'label' => 'Custom definitions', 'override' => 12, 'html' => '<div id="netdataCustomTextAce" style="height: 300px;">' . htmlentities($this->config['netdataCustom']) . '</div>']),
  186. $this->settingsOption('textbox', 'netdataCustom', ['class' => 'jsonTextarea hidden', 'id' => 'netdataCustomText', 'label' => '']),
  187. );
  188. $homepageSettings['settings']['Options'] = array(
  189. $this->settingsOption('refresh', 'homepageNetdataRefresh'),
  190. );
  191. return array_merge($homepageInformation, $homepageSettings);
  192. }
  193. public function netdataHomepagePermissions($key = null)
  194. {
  195. $permissions = [
  196. 'main' => [
  197. 'enabled' => [
  198. 'homepageNetdataEnabled'
  199. ],
  200. 'auth' => [
  201. 'homepageNetdataAuth'
  202. ],
  203. 'not_empty' => [
  204. 'netdataURL'
  205. ]
  206. ]
  207. ];
  208. return $this->homepageCheckKeyPermissions($key, $permissions);
  209. }
  210. public function homepageOrderNetdata()
  211. {
  212. if ($this->homepageItemPermissions($this->netdataHomepagePermissions('main'))) {
  213. return '
  214. <div id="' . __FUNCTION__ . '">
  215. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Netdata...</h2></div>
  216. <script>
  217. // Netdata
  218. homepageNetdata("' . $this->config['homepageNetdataRefresh'] . '");
  219. // End Netdata
  220. </script>
  221. </div>
  222. ';
  223. }
  224. }
  225. public function disk($dimension, $url)
  226. {
  227. $data = [];
  228. // Get Data
  229. $dataUrl = $url . '/api/v1/data?chart=system.io&dimensions=' . $dimension . '&format=array&points=540&group=average&gtime=0&options=absolute|jsonwrap|nonzero&after=-540';
  230. try {
  231. $options = $this->requestOptions($url, $this->config['homepageNetdataRefresh'], $this->config['netdataDisableCertCheck'], $this->config['netdataUseCustomCertificate']);
  232. $response = Requests::get($dataUrl, [], $options);
  233. if ($response->success) {
  234. $json = json_decode($response->body, true);
  235. $data['value'] = $json['latest_values'][0] / 1000;
  236. $data['percent'] = $this->getPercent($json['latest_values'][0], $json['max']);
  237. $data['units'] = 'MiB/s';
  238. $data['max'] = $json['max'];
  239. }
  240. } catch (Requests_Exception $e) {
  241. $this->setLoggerChannel('Netdata')->error($e);
  242. };
  243. return $data;
  244. }
  245. public function diskSpace($dimension, $url)
  246. {
  247. $data = [];
  248. // Get Data
  249. $dataUrl = $url . '/api/v1/data?chart=disk_space._&format=json&points=509&group=average&gtime=0&options=ms|jsonwrap|nonzero&after=-540&dimension=' . $dimension;
  250. try {
  251. $options = $this->requestOptions($url, $this->config['homepageNetdataRefresh'], $this->config['netdataDisableCertCheck'], $this->config['netdataUseCustomCertificate']);
  252. $response = Requests::get($dataUrl, [], $options);
  253. if ($response->success) {
  254. $json = json_decode($response->body, true);
  255. $data['value'] = $json['result']['data'][0][1];
  256. $data['percent'] = $data['value'];
  257. $data['units'] = '%';
  258. $data['max'] = 100;
  259. }
  260. } catch (Requests_Exception $e) {
  261. $this->setLoggerChannel('Netdata')->error($e);
  262. }
  263. return $data;
  264. }
  265. public function net($dimension, $url)
  266. {
  267. $data = [];
  268. // Get Data
  269. $dataUrl = $url . '/api/v1/data?chart=system.net&dimensions=' . $dimension . '&format=array&points=540&group=average&gtime=0&options=absolute|jsonwrap|nonzero&after=-540';
  270. try {
  271. $options = $this->requestOptions($url, $this->config['homepageNetdataRefresh'], $this->config['netdataDisableCertCheck'], $this->config['netdataUseCustomCertificate']);
  272. $response = Requests::get($dataUrl, [], $options);
  273. if ($response->success) {
  274. $json = json_decode($response->body, true);
  275. $data['value'] = $json['latest_values'][0] / 1000;
  276. $data['percent'] = $this->getPercent($json['latest_values'][0], $json['max']);
  277. $data['units'] = 'Mbit/s';
  278. $data['max'] = $json['max'];
  279. }
  280. } catch (Requests_Exception $e) {
  281. $this->setLoggerChannel('Netdata')->error($e);
  282. }
  283. return $data;
  284. }
  285. public function cpu($url)
  286. {
  287. $data = [];
  288. $dataUrl = $url . '/api/v1/data?chart=system.cpu&format=array';
  289. try {
  290. $options = $this->requestOptions($url, $this->config['homepageNetdataRefresh'], $this->config['netdataDisableCertCheck'], $this->config['netdataUseCustomCertificate']);
  291. $response = Requests::get($dataUrl, [], $options);
  292. if ($response->success) {
  293. $json = json_decode($response->body, true);
  294. $data['value'] = $json[0];
  295. $data['percent'] = $data['value'];
  296. $data['max'] = 100;
  297. $data['units'] = '%';
  298. }
  299. } catch (Requests_Exception $e) {
  300. $this->setLoggerChannel('Netdata')->error($e);
  301. }
  302. return $data;
  303. }
  304. public function ram($url)
  305. {
  306. $data = [];
  307. $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';
  308. try {
  309. $options = $this->requestOptions($url, $this->config['homepageNetdataRefresh'], $this->config['netdataDisableCertCheck'], $this->config['netdataUseCustomCertificate']);
  310. $response = Requests::get($dataUrl, [], $options);
  311. if ($response->success) {
  312. $json = json_decode($response->body, true);
  313. $data['value'] = $json['result'][0];
  314. $data['percent'] = $data['value'];
  315. $data['max'] = 100;
  316. $data['units'] = '%';
  317. }
  318. } catch (Requests_Exception $e) {
  319. $this->setLoggerChannel('Netdata')->error($e);
  320. }
  321. return $data;
  322. }
  323. public function swap($url)
  324. {
  325. $data = [];
  326. $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';
  327. try {
  328. $options = $this->requestOptions($url, $this->config['homepageNetdataRefresh'], $this->config['netdataDisableCertCheck'], $this->config['netdataUseCustomCertificate']);
  329. $response = Requests::get($dataUrl, [], $options);
  330. if ($response->success) {
  331. $json = json_decode($response->body, true);
  332. $data['value'] = $json['result'][0];
  333. $data['percent'] = $data['value'];
  334. $data['max'] = 100;
  335. $data['units'] = '%';
  336. }
  337. } catch (Requests_Exception $e) {
  338. $this->setLoggerChannel('Netdata')->error($e);
  339. }
  340. return $data;
  341. }
  342. public function getPercent($val, $max)
  343. {
  344. if ($max == 0) {
  345. return 0;
  346. } else {
  347. return ($val / $max) * 100;
  348. }
  349. }
  350. public function customNetdata($url, $id)
  351. {
  352. try {
  353. $customs = json_decode($this->config['netdataCustom'], true, 512, JSON_THROW_ON_ERROR);
  354. } catch (Exception $e) {
  355. $customs = false;
  356. }
  357. if ($customs == false) {
  358. return [
  359. 'error' => 'unable to parse custom JSON'
  360. ];
  361. } else if (!isset($customs[$id])) {
  362. return [
  363. 'error' => 'custom definition not found'
  364. ];
  365. } else {
  366. $data = [];
  367. $custom = $customs[$id];
  368. if (isset($custom['url']) && isset($custom['value'])) {
  369. if (isset($custom['netdata']) && $custom['netdata'] != '') {
  370. $url = $this->qualifyURL($custom['netdata']);
  371. }
  372. $dataUrl = $url . '/' . $custom['url'];
  373. try {
  374. $options = $this->requestOptions($url, $this->config['homepageNetdataRefresh'], $this->config['netdataDisableCertCheck'], $this->config['netdataUseCustomCertificate']);
  375. $response = Requests::get($dataUrl, [], $options);
  376. if ($response->success) {
  377. $json = json_decode($response->body, true);
  378. if (!isset($custom['max']) || $custom['max'] == '') {
  379. $custom['max'] = 100;
  380. }
  381. $data['max'] = $custom['max'];
  382. if (!isset($custom['units']) || $custom['units'] == '') {
  383. $custom['units'] = '%';
  384. }
  385. $data['units'] = $custom['units'];
  386. $selectors = explode(',', $custom['value']);
  387. foreach ($selectors as $selector) {
  388. if (is_numeric($selector)) {
  389. $selector = (int)$selector;
  390. }
  391. if (!isset($data['value'])) {
  392. $data['value'] = $json[$selector];
  393. } else {
  394. $data['value'] = $data['value'][$selector];
  395. }
  396. }
  397. if (isset($custom['mutator'])) {
  398. $data['value'] = $this->parseMutators($data['value'], $custom['mutator']);
  399. }
  400. if ($data['max'] == 0) {
  401. $data['percent'] = 0;
  402. } else {
  403. $data['percent'] = ($data['value'] / $data['max']) * 100;
  404. }
  405. }
  406. } catch (Requests_Exception $e) {
  407. $this->setLoggerChannel('Netdata')->error($e);
  408. }
  409. } else {
  410. $data['error'] = 'custom definition incomplete';
  411. }
  412. return $data;
  413. }
  414. }
  415. public function parseMutators($val, $mutators)
  416. {
  417. $mutators = explode(',', $mutators);
  418. foreach ($mutators as $m) {
  419. $op = $m[0];
  420. try {
  421. $m = (float)substr($m, 1);
  422. switch ($op) {
  423. case '+':
  424. $val = $val + $m;
  425. break;
  426. case '-':
  427. $val = $val - $m;
  428. break;
  429. case '/':
  430. $val = $val / $m;
  431. break;
  432. case '*':
  433. $val = $val * $m;
  434. break;
  435. default:
  436. break;
  437. }
  438. } catch (Exception $e) {
  439. //
  440. }
  441. }
  442. return $val;
  443. }
  444. }