Browse Source

Merge pull request #1383 from henrywhitaker3/netdata-tweaks

Added custom data sources to netdata homepage item
causefx 6 years ago
parent
commit
bf45ad1caf

+ 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',

+ 1 - 1
api/functions/homepage-functions.php

@@ -2940,7 +2940,7 @@ function getHomepageList()
 						'type' => 'html',
 						'override' => 6,
 						'label' => 'Info',
-						'html' => '<p>This homepage item requires <a href="https://github.com/henrywhitaker3/Speedtest-Tracker" target="_blank" rel="noreferrer noopener">Speedtest-Tracker</a> to be running on your network.</p>'
+						'html' => '<p>This homepage item requires <a href="https://github.com/henrywhitaker3/Speedtest-Tracker" target="_blank" rel="noreferrer noopener">Speedtest-Tracker <i class="fa fa-external-link" aria-hidden="true"></i></a> to be running on your network.</p>'
 					),
 					array(
 						'type' => 'switch',

+ 176 - 51
api/functions/netdata-functions.php

@@ -117,6 +117,86 @@ 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.</p>
+                <table class="table table-striped">
+                    <thead>
+                        <tr>
+                            <th>Parameter</th>
+                            <th>Description</th>
+                            <th>Required</th>
+                        </tr>
+                    </thead>
+                    <tbody>
+                        <tr>
+                            <td>url</td>
+                            <td>Specifies the netdata API endpoint</td>
+                            <td><i class="fa fa-check text-success" aria-hidden="true"></i></td>
+                        </tr>
+                        <tr>
+                            <td>value</td>
+                            <td>Specifies the selector used to get the data form the netdata response</td>
+                            <td><i class="fa fa-check text-success" aria-hidden="true"></i></td>
+                        </tr>
+                        <tr>
+                            <td>units</td>
+                            <td>Specifies the units shown in the graph/chart. Defaults to %</td>
+                            <td><i class="fa fa-times text-danger" aria-hidden="true"></i></td>
+                        </tr>
+                        <tr>
+                            <td>max</td>
+                            <td>Specifies the maximum possible value for the data. Defaults to 100</td>
+                            <td><i class="fa fa-times text-danger" aria-hidden="true"></i></td>
+                        </tr>
+                        <tr>
+                            <td>mutator</td>
+                            <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>
+                            <td><i class="fa fa-times text-danger" aria-hidden="true"></i></td>
+                        </tr>
+                        <tr>
+                            <td>netdata</td>
+                            <td>Can be used to override the netdata instance data is retrieved from (in the format: http://IP:PORT)</td>
+                            <td><i class="fa fa-times text-danger" aria-hidden="true"></i></td>
+                        </tr>
+                    </tbody>
+                </table>
+            </div>
+            HTML
+        ],
+        [
+            'type' => 'html',
+            'name' => 'netdataCustomTextAce',
+            'class' => 'jsonTextarea hidden',
+            'label' => 'Custom definitions',
+            'override' => 12,
+            'html' => '<div id="netdataCustomTextAce" style="height: 300px;">' . htmlentities($GLOBALS['netdataCustom']) . '</div>',
+        ],
+        [
+            'type' => 'textbox',
+            'name' => 'netdataCustom',
+            'class' => 'jsonTextarea hidden',
+            'id' => 'netdataCustomText',
+            'label' => '',
+            'value' => $GLOBALS['netdataCustom'],
+        ]
+    ];
+
     $array['settings']['Options'] =  array(
         array(
             'type' => 'select',
@@ -254,32 +334,6 @@ function swap($url)
     return $data;
 }
 
-function ipmiTemp($url, $unit)
-{
-    $data = [];
-    $dataUrl = $url . '/api/v1/data?chart=ipmi.temperatures_c&format=array&points=540&group=average&gtime=0&options=absolute|jsonwrap|nonzero&after=-540';
-    try {
-        $response = Requests::get($dataUrl);
-        if ($response->success) {
-            $json = json_decode($response->body, true);
-            $data['value'] = $json['result'][0];
-            if($unit == 'c') {
-                $data['percent'] = ($data['value'] / 50) * 100;
-                $data['max'] = 50;
-            } else if($unit == 'f') {
-                $data['value'] = ($data['value'] * 9/5) + 32;
-                $data['percent'] = ($data['value'] / 122) * 100;
-                $data['max'] = 122;
-            }
-            $data['units'] = '°'.strtoupper($unit);
-        }
-    } catch (Requests_Exception $e) {
-        writeLog('error', 'Netdata Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
-    };
-
-    return $data;
-}
-
 function getPercent($val, $max)
 {
     if($max == 0) {
@@ -289,35 +343,106 @@ function getPercent($val, $max)
     }
 }
 
-function cpuTemp($url, $unit)
+function customNetdata($url, $id)
 {
-    $data = [];
-    $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';
     try {
-        $response = Requests::get($dataUrl);
-        if ($response->success) {
-            $json = json_decode($response->body, true);
-            $vals = $json['latest_values'];
-            $vals = array_filter($vals);
-            if(count($vals) > 0) {
-                $data['value'] = array_sum($vals) / count($vals);
-            } else {
-                $data['value'] = 0;
+        $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];
+
+        if( isset($custom['url']) && isset($custom['value']) ) {
+            if( isset($custom['netdata']) && $custom['netdata'] != '' ) {
+                $url = qualifyURL($custom['netdata']);
             }
-            
-            if($unit == 'c') {
-                $data['percent'] = ($data['value'] / 50) * 100;
-                $data['max'] = 50;
-            } else if($unit == 'f') {
-                $data['value'] = ($data['value'] * 9/5) + 32;
-                $data['percent'] = ($data['value'] / 122) * 100;
-                $data['max'] = 122;
+            $dataUrl = $url . '/' . $custom['url'];
+            try {
+                $response = Requests::get($dataUrl);
+                if ($response->success) {
+                    $json = json_decode($response->body, true);
+
+                    if( !isset($custom['max']) || $custom['max'] == '' ) {
+                        $custom['max'] = 100;
+                    }
+                    $data['max'] = $custom['max'];
+
+                    if( !isset($custom['units']) || $custom['units'] == '' ) {
+                        $custom['units'] = '%';
+                    }
+                    $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(isset($custom['mutator'])) {
+                        $data['value'] = parseMutators($data['value'], $custom['mutator']);
+                    }
+    
+                    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');
+            };
+        } else {
+            $data['error'] = 'custom definition incomplete';
+        }
+    
+        return $data;
+    }
+}
+
+function parseMutators($val, $mutators)
+{
+    $mutators = explode(',', $mutators);
+    foreach($mutators as $m) {
+        $op = $m[0];
+        try {
+            $m = (float) substr($m, 1);
+            switch($op) {
+                case '+':
+                    $val = $val + $m;
+                    break;
+                case '-':
+                    $val = $val - $m;
+                    break;
+                case '/':
+                    $val = $val / $m;
+                    break;
+                case '*':
+                    $val = $val * $m;
+                    break;
+                default:
+                    break;
             }
-            $data['units'] = '°'.strtoupper($unit);
+        } catch(Exception $e) {
+            //
         }
-    } catch (Requests_Exception $e) {
-        writeLog('error', 'Netdata Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
-    };
+    }
 
-    return $data;
+    return $val;
 }

+ 3 - 15
api/functions/option-functions.php

@@ -188,21 +188,9 @@ function netdataOptions()
 			'value' => 'disk-avail',
 		],
 		[
-			'name' => 'IPMI Temperature C',
-			'value' => 'ipmi-temp-c'
-		],
-		[
-			'name' => 'IPMI Temperature F',
-			'value' => 'ipmi-temp-f'
-		],
-		[
-			'name' => 'CPU Temperature C',
-			'value' => 'cpu-temp-c'
-		],
-		[
-			'name' => 'CPU Temperature F',
-			'value' => 'cpu-temp-f'
-		],
+			'name' => 'Custom',
+			'value' => 'custom',
+		]
 	];
 }
 

+ 19 - 0
js/custom.js

@@ -1987,4 +1987,23 @@ $(document).on('click', ".ipInfo", function(){
 $(document).on('click', '.allGroupsList', function() {
     console.log($(this));
     $(this).toggleClass('active');
+});
+// Control init of custom netdata JSON editor
+$(document).on('click', 'li a[aria-controls="Custom data"]', function() {
+    var resizeEditor = function(jsonEditor) {
+        const aceEditor = jsonEditor;
+        const newHeight = aceEditor.getSession().getScreenLength() * (aceEditor.renderer.lineHeight + aceEditor.renderer.scrollBar.getWidth());
+        aceEditor.container.style.height = `${newHeight}px`;
+        aceEditor.resize();
+    }
+
+    jsonEditor = ace.edit("netdataCustomTextAce");
+    var JsonMode = ace.require("ace/mode/javascript").Mode;
+    jsonEditor.session.setMode(new JsonMode());
+    jsonEditor.setTheme("ace/theme/idle_fingers");
+    jsonEditor.setShowPrintMargin(false);
+    jsonEditor.session.on('change', function(delta) {
+        $('#netdataCustomText').val(jsonEditor.getValue());
+        $('#customize-appearance-form-save').removeClass('hidden');
+    });
 });

File diff suppressed because it is too large
+ 0 - 0
js/custom.min.js


+ 4 - 3
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);
@@ -7671,7 +7673,6 @@ function buildNetdataItem(array){
     return html;
 }
 function buildNetdata(array){
-    // console.log(array);
     var data = array.data;
     if(array === false){ return ''; }
     window.netdata = [];
@@ -7860,7 +7861,7 @@ function tryUpdateNetdata(array){
             if(window.netdata) {
                 if(window.netdata[(i+1)]) {
                     window.netdata[(i+1)].set(e.percent); // set actual value
-                    $('#gaugeChart' + (i+1) + 'Value').html(parseFloat(e.percent).toFixed(1));
+                    $('#gaugeChart' + (i+1) + 'Value').html(parseFloat(e.value).toFixed(1));
                     existing = true;
                 }
             } else {

Some files were not shown because too many files changed in this diff