diff --git a/.gitignore b/.gitignore index f43bafc..348226d 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,8 @@ src/library/sysadm-general.o src/library/sysadm-lifepreserver.o src/library/sysadm-network.o src/library/sysadm-firewall.o +src/library/sysadm-servicemanager.o +src/library/sysadm-systeminfo.o src/library/sysadm-update.o src/library/sysadm-usermanager.o src/server/AuthorizationManager.o diff --git a/api/classes/systeminfo.rst b/api/classes/systeminfo.rst new file mode 100644 index 0000000..c6cd0e2 --- /dev/null +++ b/api/classes/systeminfo.rst @@ -0,0 +1,151 @@ +.. _systeminfo: + +systeminfo +********** + +The systeminfo class is used to retrieve information about the system. Every systeminfo class request contains the following parameters: + ++---------------------------------+---------------+----------------------------------------------------------------------------------------------------------------------+ +| **Parameter** | **Value** | **Description** | +| | | | ++=================================+===============+======================================================================================================================+ +| id | | any unique value for the request; examples include a hash, checksum, or uuid | +| | | | ++---------------------------------+---------------+----------------------------------------------------------------------------------------------------------------------+ +| name | systeminfo | | +| | | | ++---------------------------------+---------------+----------------------------------------------------------------------------------------------------------------------+ +| namespace | sysadm | | +| | | | ++---------------------------------+---------------+----------------------------------------------------------------------------------------------------------------------+ +| action | | supported actions include "batteryinfo", "externalmounts" | +| | | | ++---------------------------------+---------------+----------------------------------------------------------------------------------------------------------------------+ + +The rest of this section provides examples of the available *actions* for each type of request, along with their responses. + +.. index:: batteryinfo, systeminfo + +.. _Battery Information: + +Battery Information +=================== + +The "batteryinfo" action will indicate whether or not a battery exists. If it does, it will also report its current charge percentage level (1-99) and its +status (offline, charging, on backup, or unknown). + +**REST Request** + +.. code-block:: json + + PUT /sysadm/systeminfo + { + "action" : "batteryinfo" + } + +**REST Response** + +.. code-block:: json + + { + "args": { + "batteryinfo": { + "battery": "false" + } + } + } + +**WebSocket Request** + +.. code-block:: json + + { + "namespace" : "sysadm", + "name" : "systeminfo", + "id" : "fooid", + "args" : { + "action" : "batteryinfo" + } + } + +**WebSocket Response** + +.. code-block:: json + + { + "args": { + "batteryinfo": { + "battery": "false" + } + }, + "id": "fooid", + "name": "response", + "namespace": "sysadm" + } + +.. index:: externalmounts, systeminfo + +.. _List External Mounts: + +List External Mounts +==================== + +The "externalmounts" action returns a list of mounted external devices. Supported device types are UNKNOWN, USB, HDRIVE (external hard drive), DVD, and SDCARD. +For each mounted device, the response will include the device name, filesystem, mount path, and device type. + +**REST Request** + +.. code-block:: json + + PUT /sysadm/systeminfo + { + "action" : "externalmounts" + } + +**REST Response** + +.. code-block:: json + + { + "args": { + "externalmounts": { + "/dev/fuse": { + "filesystem": "fusefs", + "path": "/usr/home/kris/.gvfs", + "type": "UNKNOWN" + } + } + } + } + +**WebSocket Request** + +.. code-block:: json + + { + "id" : "fooid", + "namespace" : "sysadm", + "name" : "systeminfo", + "args" : { + "action" : "externalmounts" + } + } + +**WebSocket Response** + +.. code-block:: json + + { + "args": { + "externalmounts": { + "/dev/fuse": { + "filesystem": "fusefs", + "path": "/usr/home/kris/.gvfs", + "type": "UNKNOWN" + } + } + }, + "id": "fooid", + "name": "response", + "namespace": "sysadm" + } \ No newline at end of file diff --git a/src/library/library.pro b/src/library/library.pro index 61c4696..b0c650e 100644 --- a/src/library/library.pro +++ b/src/library/library.pro @@ -16,6 +16,7 @@ HEADERS += sysadm-global.h \ sysadm-network.h \ sysadm-firewall.h \ sysadm-servicemanager.h\ + sysadm-systeminfo.h\ sysadm-update.h \ sysadm-usermanager.h @@ -25,6 +26,7 @@ SOURCES += NetDevice.cpp \ sysadm-network.cpp \ sysadm-firewall.cpp \ sysadm-servicemanager.cpp \ + sysadm-systeminfo.cpp \ sysadm-update.cpp \ sysadm-usermanager.cpp diff --git a/src/library/sysadm-systeminfo.cpp b/src/library/sysadm-systeminfo.cpp new file mode 100644 index 0000000..c6d14de --- /dev/null +++ b/src/library/sysadm-systeminfo.cpp @@ -0,0 +1,86 @@ +//=========================================== +// PC-BSD source code +// Copyright (c) 2015, PC-BSD Software/iXsystems +// Available under the 3-clause BSD license +// See the LICENSE file for full details +//=========================================== +#include "sysadm-general.h" +#include "sysadm-systeminfo.h" +#include "sysadm-global.h" + +using namespace sysadm; + +//PLEASE: Keep the functions in the same order as listed in pcbsd-general.h + + +//Battery Availability +QJsonObject SysInfo::batteryInfo(){ + QJsonObject retObject; + bool ok; + + int val = General::RunCommand("apm -l").toInt(&ok); + if ( ok && (val >= 0 && val <= 100) ) { + retObject.insert("battery", "true"); + } else { + retObject.insert("battery", "false"); + return retObject; + } + + // We have a battery, return info about it + //Battery Charge Level + QString tmp; + tmp.setNum(val); + retObject.insert("level", tmp); + + //Battery Charging State + int state = General::RunCommand("apm -a").toInt(&ok); + if ( ok && state == 0 ) + retObject.insert("status", "offline"); + else if ( ok && state == 1 ) + retObject.insert("status", "charging"); + else if ( ok && state == 2 ) + retObject.insert("status", "backup"); + else + retObject.insert("status", "unknown"); + + return retObject; +} + +// ==== ExternalDevicePaths() ==== +QJsonObject SysInfo::externalDevicePaths() { + QJsonObject retObject; + + //Returns: QStringList[::::::::] + //Note: = [USB, HDRIVE, DVD, SDCARD, UNKNOWN] + QStringList devs = General::RunCommand("mount").split("\n"); + + //Now check the output + for(int i=0; i +#include "sysadm-global.h" + +namespace sysadm{ + +class SysInfo{ +public: + static QJsonObject batteryInfo(); + static QJsonObject externalDevicePaths(); +}; + +} //end of pcbsd namespace + +#endif diff --git a/src/server/WebBackend.cpp b/src/server/WebBackend.cpp index 5f3c058..70f344c 100644 --- a/src/server/WebBackend.cpp +++ b/src/server/WebBackend.cpp @@ -12,6 +12,7 @@ #include "sysadm-general.h" #include "sysadm-lifepreserver.h" #include "sysadm-network.h" +#include "sysadm-systeminfo.h" #include "sysadm-update.h" #include "syscache-client.h" @@ -67,6 +68,8 @@ RestOutputStruct::ExitCode WebSocket::EvaluateBackendRequest(const RestInputStru return EvaluateSysadmNetworkRequest(IN.args, out); }else if(namesp=="sysadm" && name=="lifepreserver"){ return EvaluateSysadmLifePreserverRequest(IN.args, out); + }else if(namesp=="sysadm" && name=="systeminfo"){ + return EvaluateSysadmSystemInfoRequest(IN.args, out); }else if(namesp=="sysadm" && name=="update"){ return EvaluateSysadmUpdateRequest(IN.args, out); }else{ @@ -245,6 +248,34 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmLifePreserverRequest(const Q return RestOutputStruct::OK; } +//==== SYSADM -- SysInfo ==== +RestOutputStruct::ExitCode WebSocket::EvaluateSysadmSystemInfoRequest(const QJsonValue in_args, QJsonObject *out){ + if(in_args.isObject()){ + QStringList keys = in_args.toObject().keys(); + bool ok = false; + if(keys.contains("action")){ + QString act = JsonValueToString(in_args.toObject().value("action")); + if(act=="externalmounts"){ + ok = true; + out->insert("externalmounts", sysadm::SysInfo::externalDevicePaths()); + } + if(act=="batteryinfo"){ + ok = true; + out->insert("batteryinfo", sysadm::SysInfo::batteryInfo()); + } + + } //end of "action" key usage + + //If nothing done - return the proper code + if(!ok){ + return RestOutputStruct::BADREQUEST; + } + }else{ // if(in_args.isArray()){ + return RestOutputStruct::BADREQUEST; + } + return RestOutputStruct::OK; +} + //==== SYSADM -- Update ==== RestOutputStruct::ExitCode WebSocket::EvaluateSysadmUpdateRequest(const QJsonValue in_args, QJsonObject *out){ if(in_args.isObject()){ diff --git a/src/server/WebSocket.h b/src/server/WebSocket.h index a4a45ae..7ab88e8 100644 --- a/src/server/WebSocket.h +++ b/src/server/WebSocket.h @@ -50,6 +50,8 @@ private: RestOutputStruct::ExitCode EvaluateSysadmNetworkRequest(const QJsonValue in_args, QJsonObject *out); // -- sysadm LifePreserver API RestOutputStruct::ExitCode EvaluateSysadmLifePreserverRequest(const QJsonValue in_args, QJsonObject *out); + // -- sysadm info API + RestOutputStruct::ExitCode EvaluateSysadmSystemInfoRequest(const QJsonValue in_args, QJsonObject *out); // -- sysadm Update API RestOutputStruct::ExitCode EvaluateSysadmUpdateRequest(const QJsonValue in_args, QJsonObject *out);