Add a new API call: sysadm/zfs (action==list_pools)

This subsystem can be expanded later for any zfs/zpool command calls

REST Request:
-------------------------------
PUT /sysadm/zfs
{
   "action" : "list_pools"
}

WebSocket Request:
-------------------------------
{
   "namespace" : "sysadm",
   "args" : {
      "action" : "list_pools"
   },
   "name" : "zfs",
   "id" : "fooid"
}

Response:
-------------------------------
{
  "args": {
    "tank": {
      "alloc": "71.8G",
      "altroot": "-",
      "cap": "32%",
      "dedup": "1.00x",
      "expandsz": "-",
      "frag": "18%",
      "free": "148G",
      "health": "ONLINE",
      "size": "220G"
    }
  },
  "id": "fooid",
  "name": "response",
  "namespace": "sysadm"
}
This commit is contained in:
Ken Moore
2016-03-02 12:34:17 -05:00
parent e45b9e5101
commit a14e9a30d6
2 changed files with 43 additions and 1 deletions

View File

@@ -66,7 +66,11 @@ RestOutputStruct::ExitCode WebSocket::AvailableSubsystems(bool allaccess, QJsonO
if(QFile::exists("/usr/local/sbin/iohyve")){
out->insert("sysadm/iohyve", "read/write");
}
if(QFile::exists("/sbin/zfs") && QFile::exists("/sbin/zpool")){
out->insert("sysadm/zfs", allaccess ? "read/write" : "read");
}
// - Generic system information
out->insert("sysadm/systemmanager","read/write");
@@ -117,6 +121,8 @@ RestOutputStruct::ExitCode WebSocket::EvaluateBackendRequest(const RestInputStru
return EvaluateSysadmSystemMgmtRequest(IN.args, out);
}else if(namesp=="sysadm" && name=="update"){
return EvaluateSysadmUpdateRequest(IN.args, out);
}else if(namesp=="sysadm" && name=="zfs"){
return EvaluateSysadmZfsRequest(IN.args, out);
}else{
return RestOutputStruct::BADREQUEST;
}
@@ -628,3 +634,37 @@ RestOutputStruct::ExitCode WebSocket::EvaluateSysadmIohyveRequest(const QJsonVal
}
return RestOutputStruct::OK;
}
// ==== SYSADM ZFS API ====
RestOutputStruct::ExitCode WebSocket::EvaluateSysadmZfsRequest(const QJsonValue in_args, QJsonObject *out){
if(!in_args.isObject() || !in_args.toObject().contains("action") ){ return RestOutputStruct::BADREQUEST; }
QString act = in_args.toObject().value("action").toString();
if(act=="list_pools"){
bool ok = false;
QStringList info = sysadm::General::RunCommand(ok, "zpool list").split("\n");
if(ok && info.length()>1){ //first line is headers
//Line Format (3/2/16): Name/Size/Alloc/Free/Expandsz/Frag/Cap/Dedup/Health/Altroot
for(int i=1; i<info.length(); i++){
if(info[i].isEmpty()){ continue; }
info[i].replace("\t"," ");
QString name = info[i].section(" ",0,0,QString::SectionSkipEmpty);
QJsonObject obj;
obj.insert("size",info[i].section(" ",1,1,QString::SectionSkipEmpty) );
obj.insert("alloc",info[i].section(" ",2,2,QString::SectionSkipEmpty) );
obj.insert("free",info[i].section(" ",3,3,QString::SectionSkipEmpty) );
obj.insert("expandsz",info[i].section(" ",4,4,QString::SectionSkipEmpty) );
obj.insert("frag",info[i].section(" ",5,5,QString::SectionSkipEmpty) );
obj.insert("cap",info[i].section(" ",6,6,QString::SectionSkipEmpty) );
obj.insert("dedup",info[i].section(" ",7,7,QString::SectionSkipEmpty) );
obj.insert("health",info[i].section(" ",8,8,QString::SectionSkipEmpty) );
obj.insert("altroot",info[i].section(" ",9,9,QString::SectionSkipEmpty) );
out->insert(name,obj);
}
}
}else{
//unknown action
return RestOutputStruct::BADREQUEST;
}
return RestOutputStruct::OK;
}

View File

@@ -65,6 +65,8 @@ private:
RestOutputStruct::ExitCode EvaluateSysadmSystemMgmtRequest(const QJsonValue in_args, QJsonObject *out);
// -- sysadm Update API
RestOutputStruct::ExitCode EvaluateSysadmUpdateRequest(const QJsonValue in_args, QJsonObject *out);
// -- sysadm ZFS API
RestOutputStruct::ExitCode EvaluateSysadmZfsRequest(const QJsonValue in_args, QJsonObject *out);
private slots:
void checkIdle(); //see if the currently-connected client is idle