⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 osinfo.cpp

📁 Pegasus is an open-source implementationof the DMTF CIM and WBEM standards. It is designed to be por
💻 CPP
📖 第 1 页 / 共 3 页
字号:
      if (propertyName.equal (CIMName ("CSName")))      {         inst.getProperty(j).getValue().get(osCSName);      }  // end if CSName      else if (propertyName.equal (CIMName ("Name")))      {         inst.getProperty(j).getValue().get(osName);      }  // end if Name      else if (propertyName.equal (CIMName ("NumberOfProcesses")))      {         Uint32 propertyValue;         inst.getProperty(j).getValue().get(propertyValue);         char tmpString[80];         sprintf(tmpString, "%d processes", propertyValue);         osNumberOfProcesses.assign(tmpString);      }  // end if NumberOfProcesses      else if (propertyName.equal (CIMName ("NumberOfUsers")))      {         Uint32 propertyValue;         inst.getProperty(j).getValue().get(propertyValue);         char tmpString[80];         sprintf(tmpString, "%d users", propertyValue);         osNumberOfUsers.assign(tmpString);      }  // end if NumberOfUsers      if (propertyName.equal (CIMName ("Version")))      {         inst.getProperty(j).getValue().get(osVersion);      }  // end if Version      else if (propertyName.equal (CIMName ("OperatingSystemCapability")))      {         inst.getProperty(j).getValue().get(osCapability);      }   // end if OSCapability      else if (propertyName.equal (CIMName ("OtherTypeDescription")))      {         inst.getProperty(j).getValue().get(osOtherInfo);      }   // end if OtherTypeDescription      else if (propertyName.equal (CIMName ("NumberOfLicensedUsers")))      {         Uint32 propertyValue;         inst.getProperty(j).getValue().get(propertyValue);         // special consideration for HP-UX         if (propertyValue == 0)         {            if (String::equalNoCase(osVersion,"HP-UX"))            {               osLicensedUsers.assign("128, 256, or unlimited users");            }            else            {               osLicensedUsers.assign("Unlimited user license");            }         }  // end if 0 as number of licensed users         else  // standard number of users         {            char users[80];            sprintf(users, "%d users", propertyValue);            osLicensedUsers.assign(users);         }      }   // end if NumberOfLicensedUsers      else if (propertyName.equal (CIMName ("LastBootUpTime")))      {         CIMDateTime bdate;         char bdateString[80];         inst.getProperty(j).getValue().get(bdate);         CString dtStr = bdate.toString().getCString();         if (!cimFormat)         { // else leave in raw CIM            formatCIMDateTime(dtStr, bdateString);         }         else         {            sprintf(bdateString,"%s",(const char*)dtStr);         }         osBootUpTime.assign(bdateString);      }   // end if LastBootUpTime      else if (propertyName.equal (CIMName ("LocalDateTime")))      {         CIMDateTime ldate;         char ldateString[80];         inst.getProperty(j).getValue().get(ldate);         CString dtStr = ldate.toString().getCString();         if (!cimFormat)         { // else leave in raw CIM            formatCIMDateTime(dtStr, ldateString);         }         else         {            sprintf(ldateString,"%s",(const char*)dtStr);         }         osLocalDateTime.assign(ldateString);      }   // end if LocalDateTime      else if (propertyName.equal (CIMName ("SystemUpTime")))      {         Uint64 total;         char   uptime[80];         inst.getProperty(j).getValue().get(total);         if (!cimFormat)         { // else leave in raw CIM            // let's make things a bit easier for our user to read            Uint64 days = 0;            Uint32 hours = 0;            Uint32 minutes = 0;            Uint32 seconds = 0;            Uint64 totalSeconds = total;            seconds = Uint32(total%60);            total = total/60;            minutes = Uint32(total%60);            total = total/60;            hours = Uint32(total%24);            total = total/24;            days = total;            // now deal with the proper singular/plural            char dayString[20];            char hourString[20];            char minuteString[20];            char secondString[20];            sprintf(dayString,                (days == 0 ? "" : (days == 1 ? "1 day," :                    "%" PEGASUS_64BIT_CONVERSION_WIDTH "u days,")), days);            // for other values, want to display the 0s            sprintf(hourString, (hours == 1 ? "1 hr," : "%u hrs,"), hours);            sprintf(minuteString,                (minutes == 1 ? "1 min," : "%u mins,"), minutes);            sprintf(secondString,                (seconds == 1 ? "1 sec" : "%u secs"), seconds);            sprintf(uptime,                "%" PEGASUS_64BIT_CONVERSION_WIDTH "u seconds = %s %s %s %s",                totalSeconds,                dayString,                hourString,                minuteString,                secondString);            osSystemUpTime.assign(uptime);         }  // end of if wanted nicely formatted vs. raw CIM         else         {            sprintf(uptime, "%" PEGASUS_64BIT_CONVERSION_WIDTH "u", total);         }         osSystemUpTime.assign(uptime);      }   // end if SystemUpTime   }  // end of for looping through properties}/**   displayProperties method of the osinfo Test Client  */void OSInfoCommand::displayProperties(ostream& outPrintWriter){   // interesting properties are stored off in class variables   outPrintWriter << "OperatingSystem Information" << endl;   // expect to have values for the keys (even if Unknown)   outPrintWriter << "  Host: " << osCSName << endl;   outPrintWriter << "  Name: " << osName << endl;   // on Linux, the OtherTypeDescription field had distribution info   // wrote to display this info whenever it's present (any OS)   if (osOtherInfo != String::EMPTY)   {      // put in parens after the name      outPrintWriter << "   ( " << osOtherInfo << " ) " << endl;   }   if (osVersion != String::EMPTY)      outPrintWriter << "  Version: " << osVersion << endl;   else      outPrintWriter << "  Version: Unknown" << endl;   if (osLicensedUsers != String::EMPTY)      outPrintWriter << "  UserLicense: " << osLicensedUsers << endl;   else      outPrintWriter << "  UserLicense: Unknown" << endl;   if (osNumberOfUsers != String::EMPTY)      outPrintWriter << "  Number of Users: " << osNumberOfUsers << endl;   else      outPrintWriter << "  Number of Users: Unknown" << endl;   if (osNumberOfProcesses != String::EMPTY)      outPrintWriter << "  Number of Processes: " << osNumberOfProcesses << endl;   else      outPrintWriter << "  Number of Processes: Unknown" << endl;   if (osCapability != String::EMPTY)      outPrintWriter << "  OSCapability: " << osCapability << endl;   else      outPrintWriter << "  OSCapability: Unknown" << endl;   if (osBootUpTime != String::EMPTY)      outPrintWriter << "  LastBootTime: " << osBootUpTime << endl;   else      outPrintWriter << "  LastBootTime: Unknown" << endl;   if (osLocalDateTime != String::EMPTY)      outPrintWriter << "  LocalDateTime: " << osLocalDateTime << endl;   else      outPrintWriter << "  LocalDateTime: Unknown" << endl;   if (osSystemUpTime != String::EMPTY)      outPrintWriter << "  SystemUpTime: " << osSystemUpTime << endl;   else      outPrintWriter << "  SystemUpTime: Unknown" << endl;}void OSInfoCommand::getOSInfo(ostream& outPrintWriter,                              ostream& errPrintWriter){    CIMClient client;    client.setTimeout( _timeout );    try    {        _connectToServer( client, outPrintWriter);        Boolean deepInheritance = true;        Boolean localOnly = false;        Boolean includeQualifiers = false;        Boolean includeClassOrigin = false;        Uint32 numberInstances;        Array<CIMInstance> cimNInstances =               client.enumerateInstances(NAMESPACE, CLASSNAME,                                         deepInheritance,                                         localOnly,  includeQualifiers,                                         includeClassOrigin );        numberInstances = cimNInstances.size();        // while we only have one instance (the running OS), we can take the        // first instance.  When the OSProvider supports installed OSs as well,        // will need to select the runningOS instance        for (Uint32 i = 0; i < cimNInstances.size(); i++)        {           CIMObjectPath instanceRef = cimNInstances[i].getPath ();           if ( !(instanceRef.getClassName().equal (CIMName (CLASSNAME))))           {              errorExit(errPrintWriter, "EnumerateInstances failed");           }           // first gather the interesting properties           gatherProperties(cimNInstances[i], _useRawDateTimeFormat);           // then display them           displayProperties(outPrintWriter);      }   // end for looping through instances    }  // end try    catch(const Exception& e)    {      errorExit(errPrintWriter, e.getMessage());    }}/**    Executes the command and writes the results to the PrintWriters.    @param   outPrintWriter     the ostream to which output should be                                written    @param   errPrintWriter     the ostream to which error output should be                                written    @return  0                  if the command is successful             1                  if an error occurs in executing the command */Uint32 OSInfoCommand::execute (ostream& outPrintWriter,                                 ostream& errPrintWriter){    if ( _operationType == OPERATION_TYPE_HELP )    {        cerr << usage << endl;        return (RC_SUCCESS);    }    else if ( _operationType == OPERATION_TYPE_VERSION )    {        cerr << "Version " << PEGASUS_PRODUCT_VERSION << endl;        return (RC_SUCCESS);    }    try    {        OSInfoCommand::getOSInfo( outPrintWriter, errPrintWriter );    }    catch (const OSInfoException& e)    {        errPrintWriter << OSInfoCommand::COMMAND_NAME << ": " <<            e.getMessage () << endl;        return (RC_ERROR);    }    return (RC_SUCCESS);}/**    Parses the command line, and executes the command.    @param   argc  the number of command line arguments    @param   argv  the string vector of command line arguments    @return  0                  if the command is successful             1                  if an error occurs in executing the command */PEGASUS_NAMESPACE_END// exclude main from the Pegasus NamespacePEGASUS_USING_PEGASUS;PEGASUS_USING_STD;int main (int argc, char* argv []){    OSInfoCommand    command = OSInfoCommand ();    int                rc;    MessageLoader::setPegasusMsgHomeRelative(argv[0]);    try    {        command.setCommand (argc, argv);    }    catch (const CommandFormatException& cfe)    {        String msg(cfe.getMessage());        cerr << OSInfoCommand::COMMAND_NAME << ": " << msg <<  endl;        if (msg.find(String("Unknown flag")) != PEG_NOT_FOUND)         {           MessageLoaderParms parms(ERR_OPTION_NOT_SUPPORTED_KEY,ERR_OPTION_NOT_SUPPORTED);              parms.msg_src_path = MSG_PATH;           cerr << OSInfoCommand::COMMAND_NAME <<             ": " << MessageLoader::getMessage(parms) << endl;         }        else         {           MessageLoaderParms parms(ERR_USAGE_KEY,ERR_USAGE);              parms.msg_src_path = MSG_PATH;           cerr << OSInfoCommand::COMMAND_NAME <<             ": " << MessageLoader::getMessage(parms) << endl;         }        exit (Command::RC_ERROR);    }    rc = command.execute (cout, cerr);    exit (rc);    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -