Ver Fonte

Added custom data sources

Henry Whitaker há 6 anos atrás
pai
commit
465e325421

+ 4 - 1
api/config/default.php

@@ -381,5 +381,8 @@ return array(
 	'netdata4Enabled' => false,
 	'netdata5Enabled' => false,
 	'netdata6Enabled' => false,
-	'netdata7Enabled' => false
+	'netdata7Enabled' => false,
+	'netdataCustom' => '{
+    
+	}'
 );

+ 3 - 0
api/functions/homepage-connect-functions.php

@@ -2800,6 +2800,9 @@ function getNetdata()
 					case 'cpu-temp-f':
 						$data = cpuTemp($url, 'f');
 						break;
+					case 'custom':
+						$data = customNetdata($url, $i);
+						break;
 					default:
 						$data = [
 							'title' => 'DNC',

+ 91 - 0
api/functions/netdata-functions.php

@@ -117,6 +117,44 @@ function netdataSettngsArray()
         );
     }
 
+    $array['settings']['Custom data'] = [
+        [
+            'type' => 'html',
+            'label' => '',
+            'override' => 12,
+            'html' => <<<HTML
+            <div>
+                <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>
+                <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's custom data source to RAM percentage:</p>
+                <pre>{
+                "1": {
+                    "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",
+                    "value": "result,0",
+                    "units": "%",
+                    "max": 100
+                }
+            }</pre>
+                <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. So the value field here will access data from an array like this:</p>
+                <pre>[
+                'result': [
+                    VALUE,
+                    // more values...
+                ]
+            ]</pre>
+            </div>
+            HTML
+        ],
+        [
+            'type' => 'textbox',
+            'name' => 'netdataCustom',
+            'class' => 'javaTextarea',
+            'label' => 'Custom definitions',
+            'override' => 12,
+            'value' => $GLOBALS['netdataCustom'],
+            'attr' => 'rows="10"',
+        ]
+    ];
+
     $array['settings']['Options'] =  array(
         array(
             'type' => 'select',
@@ -320,4 +358,57 @@ function cpuTemp($url, $unit)
     };
 
     return $data;
+}
+
+function customNetdata($url, $id)
+{
+    try {
+        $customs = json_decode($GLOBALS['netdataCustom'], true, 512, JSON_THROW_ON_ERROR);
+    } catch(Exception $e) {
+        $customs = false;
+    }
+        
+    if($customs == false) {
+        return [
+            'error' => 'unable to parse custom JSON'
+        ];
+    } else if(!isset($customs[$id])) {
+        return [
+            'error' => 'custom definition not found'
+        ];
+    } else {
+        $data = [];
+        $custom = $customs[$id];
+        $dataUrl = $url . '/' . $custom['url'];
+        try {
+            $response = Requests::get($dataUrl);
+            if ($response->success) {
+                $json = json_decode($response->body, true);
+                $data['max'] = $custom['max'];
+                $data['units'] = $custom['units'];
+
+                $selectors = explode(',', $custom['value']);
+                foreach($selectors as $selector) {
+                    if(is_numeric($selector)) {
+                        $selector = (int) $selector;
+                    }
+                    if(!isset($data['value'])) {
+                        $data['value'] = $json[$selector];
+                    } else {
+                        $data['value'] = $data['value'][$selector];
+                    }
+                }
+
+                if($data['max'] == 0) {
+                    $data['percent'] = 0;
+                } else {
+                    $data['percent'] = ( $data['value'] / $data['max'] ) * 100;
+                }
+            }
+        } catch (Requests_Exception $e) {
+            writeLog('error', 'Netdata Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
+        };
+    
+        return $data;
+    }
 }

+ 4 - 0
api/functions/option-functions.php

@@ -203,6 +203,10 @@ function netdataOptions()
 			'name' => 'CPU Temperature F',
 			'value' => 'cpu-temp-f'
 		],
+		[
+			'name' => 'Custom',
+			'value' => 'custom',
+		]
 	];
 }
 

+ 3 - 1
js/functions.js

@@ -7661,7 +7661,9 @@ function buildNetdataItem(array){
         }
         display += ' ';
 
-        if(e.chart == 'easypiechart') {
+        if(e.error) {
+            console.log('Netdata error (Chart ' + (i+1) + '): ' + e.error);
+        } else if(e.chart == 'easypiechart') {
             html += buildEasyPieChart(e,i,size,easySize,display);
         } else if(e.chart == 'gauge') {
             html += buildGaugeChart(e,i,size,easySize,display);