Henry Whitaker 6 лет назад
Родитель
Сommit
64f2c4edc5

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

@@ -2794,6 +2794,12 @@ function getNetdata()
 					case 'ipmi-temp-f':
 						$data = ipmiTemp($url, 'f');
 						break;
+					case 'cpu-temp-c':
+						$data = cpuTemp($url, 'c');
+						break;
+					case 'cpu-temp-f':
+						$data = cpuTemp($url, 'f');
+						break;
 					default:
 						$data = [
 							'title' => 'DNC',

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

@@ -287,4 +287,37 @@ function getPercent($val, $max)
     } else {
         return ( $val / $max ) * 100;
     }
+}
+
+function cpuTemp($url, $unit)
+{
+    $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;
+            }
+            
+            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;
 }

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

@@ -195,6 +195,14 @@ function netdataOptions()
 			'name' => 'IPMI Temperature F',
 			'value' => 'ipmi-temp-f'
 		],
+		[
+			'name' => 'CPU Temperature C',
+			'value' => 'cpu-temp-c'
+		],
+		[
+			'name' => 'CPU Temperature F',
+			'value' => 'cpu-temp-f'
+		],
 	];
 }