2024-09-07 20:53:46 +10:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Nut class
|
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
|
|
|
* @category PHP
|
|
|
|
* @package PSI_UPS
|
|
|
|
* @author Artem Volk <artvolk@mail.ru>
|
|
|
|
* @author Anders Häggström <hagge@users.sourceforge.net>
|
|
|
|
* @copyright 2009 phpSysInfo
|
2025-05-14 16:14:01 +01:00
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
|
2024-09-07 20:53:46 +10:00
|
|
|
* @version SVN: $Id: class.nut.inc.php 661 2012-08-27 11:26:39Z namiltd $
|
|
|
|
* @link http://phpsysinfo.sourceforge.net
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* getting ups information from upsc program
|
|
|
|
*
|
|
|
|
* @category PHP
|
|
|
|
* @package PSI_UPS
|
|
|
|
* @author Artem Volk <artvolk@mail.ru>
|
|
|
|
* @author Anders Häggström <hagge@users.sourceforge.net>
|
|
|
|
* @copyright 2009 phpSysInfo
|
2025-05-14 16:14:01 +01:00
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License version 2, or (at your option) any later version
|
2024-09-07 20:53:46 +10:00
|
|
|
* @version Release: 3.0
|
|
|
|
* @link http://phpsysinfo.sourceforge.net
|
|
|
|
*/
|
|
|
|
class Nut extends UPS
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* internal storage for all gathered data
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $_output = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get all information from all configured ups and store output in internal array
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
2025-05-14 16:14:01 +01:00
|
|
|
if (!defined('PSI_UPS_NUT_ACCESS')) {
|
|
|
|
define('PSI_UPS_NUT_ACCESS', false);
|
|
|
|
}
|
|
|
|
switch (strtolower(PSI_UPS_NUT_ACCESS)) {
|
|
|
|
case 'data':
|
|
|
|
if (defined('PSI_UPS_NUT_LIST') && is_string(PSI_UPS_NUT_LIST)) {
|
|
|
|
if (preg_match(ARRAY_EXP, PSI_UPS_NUT_LIST)) {
|
|
|
|
$upss = eval(PSI_UPS_NUT_LIST);
|
|
|
|
} else {
|
|
|
|
$upss = array(PSI_UPS_NUT_LIST);
|
|
|
|
}
|
2024-09-07 20:53:46 +10:00
|
|
|
} else {
|
2025-05-14 16:14:01 +01:00
|
|
|
$upss = array('UPS');
|
2024-09-07 20:53:46 +10:00
|
|
|
}
|
2025-05-14 16:14:01 +01:00
|
|
|
$un = 0;
|
|
|
|
foreach ($upss as $ups) {
|
|
|
|
$temp = "";
|
|
|
|
CommonFunctions::rftsdata("upsnut{$un}.tmp", $temp);
|
|
|
|
if (! empty($temp)) {
|
|
|
|
$this->_output[$ups] = $temp;
|
|
|
|
}
|
|
|
|
$un++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (defined('PSI_UPS_NUT_LIST') && is_string(PSI_UPS_NUT_LIST)) {
|
|
|
|
if (preg_match(ARRAY_EXP, PSI_UPS_NUT_LIST)) {
|
|
|
|
$upses = eval(PSI_UPS_NUT_LIST);
|
|
|
|
} else {
|
|
|
|
$upses = array(PSI_UPS_NUT_LIST);
|
|
|
|
}
|
|
|
|
foreach ($upses as $ups) {
|
|
|
|
CommonFunctions::executeProgram('upsc', '-l '.trim($ups), $output, PSI_DEBUG);
|
|
|
|
$ups_names = preg_split("/\n/", $output, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
|
foreach ($ups_names as $ups_name) {
|
|
|
|
$upsname = trim($ups_name).'@'.trim($ups);
|
|
|
|
$temp = "";
|
|
|
|
CommonFunctions::executeProgram('upsc', $upsname, $temp, PSI_DEBUG);
|
|
|
|
if (! empty($temp)) {
|
|
|
|
$this->_output[$upsname] = $temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { //use default if address and port not defined
|
|
|
|
if (!defined('PSI_EMU_HOSTNAME') || defined('PSI_EMU_PORT')) {
|
|
|
|
CommonFunctions::executeProgram('upsc', '-l', $output, PSI_DEBUG);
|
|
|
|
} else {
|
|
|
|
CommonFunctions::executeProgram('upsc', '-l '.PSI_EMU_HOSTNAME, $output, PSI_DEBUG);
|
|
|
|
}
|
2024-09-07 20:53:46 +10:00
|
|
|
$ups_names = preg_split("/\n/", $output, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
|
foreach ($ups_names as $ups_name) {
|
2025-05-14 16:14:01 +01:00
|
|
|
$temp = "";
|
|
|
|
CommonFunctions::executeProgram('upsc', trim($ups_name), $temp, PSI_DEBUG);
|
2024-09-07 20:53:46 +10:00
|
|
|
if (! empty($temp)) {
|
2025-05-14 16:14:01 +01:00
|
|
|
$this->_output[trim($ups_name)] = $temp;
|
2024-09-07 20:53:46 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* parse the input and store data in resultset for xml generation
|
|
|
|
*
|
2025-05-14 16:14:01 +01:00
|
|
|
* @return void
|
2024-09-07 20:53:46 +10:00
|
|
|
*/
|
|
|
|
private function _info()
|
|
|
|
{
|
|
|
|
if (! empty($this->_output)) {
|
2025-05-14 16:14:01 +01:00
|
|
|
foreach ($this->_output as $name => $value) {
|
2024-09-07 20:53:46 +10:00
|
|
|
$temp = preg_split("/\n/", $value, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
|
$ups_data = array();
|
2025-05-14 16:14:01 +01:00
|
|
|
foreach ($temp as $valueTemp) {
|
|
|
|
$line = preg_split('/: /', $valueTemp, 2);
|
2024-09-07 20:53:46 +10:00
|
|
|
$ups_data[$line[0]] = isset($line[1]) ? trim($line[1]) : '';
|
|
|
|
}
|
|
|
|
$dev = new UPSDevice();
|
|
|
|
//General
|
|
|
|
$dev->setName($name);
|
|
|
|
if (isset($ups_data['ups.model'])) {
|
|
|
|
$dev->setModel($ups_data['ups.model']);
|
|
|
|
}
|
|
|
|
if (isset($ups_data['driver.name'])) {
|
|
|
|
$dev->setMode($ups_data['driver.name']);
|
|
|
|
}
|
|
|
|
if (isset($ups_data['ups.status'])) {
|
|
|
|
$dev->setStatus($ups_data['ups.status']);
|
|
|
|
}
|
2025-05-14 16:14:01 +01:00
|
|
|
if (isset($ups_data['ups.beeper.status'])) {
|
|
|
|
$dev->setBeeperStatus($ups_data['ups.beeper.status']);
|
|
|
|
}
|
2024-09-07 20:53:46 +10:00
|
|
|
|
|
|
|
//Line
|
|
|
|
if (isset($ups_data['input.voltage'])) {
|
|
|
|
$dev->setLineVoltage($ups_data['input.voltage']);
|
|
|
|
}
|
|
|
|
if (isset($ups_data['input.frequency'])) {
|
|
|
|
$dev->setLineFrequency($ups_data['input.frequency']);
|
|
|
|
}
|
|
|
|
if (isset($ups_data['ups.load'])) {
|
|
|
|
$dev->setLoad($ups_data['ups.load']);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Battery
|
|
|
|
if (isset($ups_data['battery.voltage'])) {
|
|
|
|
$dev->setBatteryVoltage($ups_data['battery.voltage']);
|
|
|
|
}
|
|
|
|
if (isset($ups_data['battery.charge'])) {
|
|
|
|
$dev->setBatterCharge($ups_data['battery.charge']);
|
|
|
|
}
|
|
|
|
if (isset($ups_data['battery.runtime'])) {
|
|
|
|
$dev->setTimeLeft(round($ups_data['battery.runtime']/60, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
//Temperature
|
|
|
|
if (isset($ups_data['ups.temperature'])) {
|
|
|
|
$dev->setTemperatur($ups_data['ups.temperature']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->upsinfo->setUpsDevices($dev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the information
|
|
|
|
*
|
|
|
|
* @see PSI_Interface_UPS::build()
|
|
|
|
*
|
2025-05-14 16:14:01 +01:00
|
|
|
* @return void
|
2024-09-07 20:53:46 +10:00
|
|
|
*/
|
|
|
|
public function build()
|
|
|
|
{
|
|
|
|
$this->_info();
|
|
|
|
}
|
|
|
|
}
|