Code: Select all
function getStringBetween($str,$from,$to)
{
$sub = substr($str, strpos($str,$from)+strlen($from),strlen($str));
return substr($sub,0,strpos($sub,$to));
}
function snmp_nas() /* https://global.download.synology.com/download/Document/MIBGuide/Synology_DiskStation_MIB_Guide.pdf */
{
if (status('Synology')=='On')
{
$to_snmp = array(
array( 'Device' => "Synology HD1 Status", 'Address' => "1.3.6.1.4.1.6574.2.1.1.5.0", 'Type' => "Diskstatus" ),
array( 'Device' => "Synology HD2 Status", 'Address' => "1.3.6.1.4.1.6574.2.1.1.5.1", 'Type' => "Diskstatus" ),
array( 'Device' => "Synology HD3 Status", 'Address' => "1.3.6.1.4.1.6574.2.1.1.5.2", 'Type' => "Diskstatus" ),
array( 'Device' => "Synology HD4 Status", 'Address' => "1.3.6.1.4.1.6574.2.1.1.5.3", 'Type' => "Diskstatus" ),
array( 'Device' => "Synology RAID Status", 'Address' => "1.3.6.1.4.1.6574.3.1.1.3.0", 'Type' => "Raidstatus" ),
array( 'Device' => "Synology Power Status", 'Address' => "1.3.6.1.4.1.6574.1.3.0", 'Type' => "Status" ),
array( 'Device' => "Synology Fan Status", 'Address' => "1.3.6.1.4.1.6574.1.4.2.0", 'Type' => "Status" ),
array( 'Device' => "Synology UPS Status", 'Address' => "1.3.6.1.4.1.6574.4.2.1.0", 'Type' => "UPSstatus" ),
array( 'Device' => "CpuUser", 'Address' => "1.3.6.1.4.1.2021.11.9.0", 'Type' => "Calculate" ), // Calculate device only
array( 'Device' => "CpuSystem", 'Address' => "1.3.6.1.4.1.2021.11.10.0", 'Type' => "Calculate" ), // Calculate device only
array( 'Device' => "Memtotal", 'Address' => "1.3.6.1.4.1.2021.4.5.0", 'Type' => "Calculate" ), // Calculate device only
array( 'Device' => "MemShared", 'Address' => "1.3.6.1.4.1.2021.4.13.0", 'Type' => "Calculate" ), // Calculate device only
array( 'Device' => "MemBuffer", 'Address' => "1.3.6.1.4.1.2021.4.14.0", 'Type' => "Calculate" ), // Calculate device only
array( 'Device' => "MemCached", 'Address' => "1.3.6.1.4.1.2021.4.15.0", 'Type' => "Calculate" ), // Calculate device only
array( 'Device' => "MemAvailable", 'Address' => "1.3.6.1.4.1.2021.4.6.0", 'Type' => "Calculate" ), // Calculate device only
array( 'Device' => "Synology HD1 Temperature", 'Address' => "1.3.6.1.4.1.6574.2.1.1.6.0" ),
array( 'Device' => "Synology HD2 Temperature", 'Address' => "1.3.6.1.4.1.6574.2.1.1.6.1" ),
array( 'Device' => "Synology HD3 Temperature", 'Address' => "1.3.6.1.4.1.6574.2.1.1.6.2" ),
array( 'Device' => "Synology HD4 Temperature", 'Address' => "1.3.6.1.4.1.6574.2.1.1.6.3" ),
array( 'Device' => "Synology System Temperature", 'Address' => "1.3.6.1.4.1.6574.1.2.0" )
);
foreach ($to_snmp as $snmp)
{
$result = snmp2_get(snmp_ip_nas, snmp_community, $snmp['Address']); // These results need cleaning ;-)
if (strpos($result, "Opaque: Float: ") === 0) // First remove the floats
{
$result = str_replace("Opaque: Float: ","",$result);
$result = substr($result, 0, strpos($result, "."));
}
elseif (strpos($result, 'STRING: "') === 0) // THe UPS Status is a string we need to retrieve the data here
{
$from = '"';
$to = '"';
$result = getStringBetween($result,$from,$to);
if ($result === "OL") $result = '1:Online'; // UPS status (OL=on line, OB=on battery, CHRG=charging, DISCHRG=discharging, etc
elseif ($result === "OB") $result = '3:On battery';
elseif ($result === "CHRG") $result = '2:Charging';
elseif ($result === "DISCHRG") $result = '4:Discharging';
else $result = '0:unknown';
}
else $result = preg_replace('/[^0-9]/', '', $result); // For everything else strip all non Alpha characters so we have only intergers left
$memory[$snmp['Device']] = $result; // Create a new array with parsed results as we can use this later for calculation
if (isset($snmp['Type'])) // For all devices that report numeric and we want to see a text as well
{
switch ($snmp['Type'])
{
case 'Status': // Simple Status Normal / Failed
if ($result == 1) $result = '1:Normal';
else $result = '2:Failed';
break;
case 'Diskstatus': // Synology disk status Each meanings of status represented describe below.
if ($result == 1) $result = '1:Normal'; // Normal(1): The hard disk functions normally.
elseif ($result == 2) $result = '2:Initialized'; // Initialized(2): The hard disk has system partition but no data.
elseif ($result == 3) $result = '3:NotInitialized'; // NotInitialized(3): The hard disk does not have system in system partition.
elseif ($result == 4) $result = '4:SystemPartitionFailed'; // SystemPartitionFailed(4): The system partitions on the hard disks are damaged.
else $result = '4:Crashed'; // Crashed(5): The hard disk has damaged.
break;
case 'Raidstatus': // Synology Raid status Each meanings of status represented describe below.
if ($result == 1) $result = '1:Normal'; // The raid functions normally. 1-4 is used for Alert Sensor in Domoticz
elseif ($result == 2) $result = '2:Repairing';
elseif ($result == 3) $result = '3:Migrating';
elseif ($result == 4) $result = '3:Expanding';
elseif ($result == 5) $result = '3:Deleting';
elseif ($result == 6) $result = '3:Creating';
elseif ($result == 7) $result = '3:RaidSyncing';
elseif ($result == 8) $result = '3:RaidParityChecking';
elseif ($result == 9) $result = '3:RaidAssembling';
elseif ($result == 10) $result = '3:Canceling';
elseif ($result == 11) $result = '4:Degrade'; // Degrade happens when a tolerable failure of disk(s) occurs.
else $result = '4:Crashed'; // Raid has crashed and just uses for read-only operation.
break;
default:
$result = $result;
}
} // All data is set, we can now determine how to update the devices in domoticz
if (!isset($snmp['Type'])) // No type is set like for temperature sensors
{
if (status($snmp['Device'])!=$result) // Retrieve the status from domoticz an compare it with current value
{
ud($snmp['Device'],0,$result);
lg('SNMP: '.$snmp['Device'].' of domoticz is: '.status($snmp['Device']).' the retrieved result is: '.$result);
}
}
elseif ($snmp['Type']=='Diskstatus' || $snmp['Type']=='Raidstatus' || $snmp['Type']=='UPSstatus' || $snmp['Type']=='Status') // Specific types need different type of domoticz sensor
{
$result = explode(":",$result); // We parsed a string like 1:Normal, we need to separate the values
if (status($snmp['Device'])!=$result[1]) // Retrieve the status from domoticz an compare it with current value
{
lg('SNMP: '.$snmp['Device'].' with '.$snmp['Type'].' of domoticz is: '.status($snmp['Device']).' the retrieved result is: '.$result[0].' '.$result[1]);
ud($snmp['Device'],$result[0],$result[1]);
}
//else lg('SNMP: Debug: '.$snmp['Device'].' with '.$snmp['Type'].' of domoticz is: '.status($snmp['Device']).' the retrieved result is: '.$result[0].' '.$result[1]);
}
//else lg('SNMP: Nothing do do here for: '.$snmp['Device'].' with '.$snmp['Type']);
//else lg('SNMP: '.$snmp['Device'].' The status seems to be the same as value is: '.$result.' the retrieved value is: '.status($snmp['Device']));
} // End of For each loop
// --------------------------------------------------------------------------------
// CPU Usage: Some values require calculation, we can do it here with the new array.
$cpuuse = ($memory['CpuUser'] + $memory['CpuSystem']);
if (status('Synology CPU Usage')!=$cpuuse)
{
ud('Synology CPU Usage',0,$cpuuse);
lg('SNMP: CPU usage of domoticz is: '.status('Synology CPU Usage').' the calculated result is: '.$cpuuse);
}
// Memory Usage Some values require calculation, we can do it here with the new array.
$MemFREE = ($memory['MemAvailable'] + $memory['MemShared'] + $memory['MemBuffer'] + $memory['MemCached']);
$MemUsepercent = (($MemFREE * 100) / $memory['Memtotal']);
$MemUsepercent = substr($MemUsepercent, 0, strpos($MemUsepercent, ".")); // Round the percentage
if (status('Synology Memory Free')!=$MemUsepercent)
{
ud('Synology Memory Free',0,$MemUsepercent);
lg('SNMP: Synology Memory free of domoticz is: '.status('Synology Memory Free').' the calculated result is: '.$MemUsepercent);
}
}
else lg('SNMP: Synology is: '.status('Synology').' unable to execute!');
}
Disk usage seems a problem with my 412+ pfff IOD not found things like that bleah.