operatingsystem_hpux.cpp
来自「Pegasus is an open-source implementation」· C++ 代码 · 共 1,015 行 · 第 1/2 页
CPP
1,015 行
//%2006//////////////////////////////////////////////////////////////////////////// Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development// Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.// Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation, The Open Group.// Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;// IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; VERITAS Software Corporation; The Open Group.// Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;// EMC Corporation; Symantec Corporation; The Open Group.//// Permission is hereby granted, free of charge, to any person obtaining a copy// of this software and associated documentation files (the "Software"), to// deal in the Software without restriction, including without limitation the// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or// sell copies of the Software, and to permit persons to whom the Software is// furnished to do so, subject to the following conditions:// // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN// ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED// "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.////==============================================================================//// Author: Jim Metcalfe, Hewlett-Packard Company (jim_metcalfe@hp.com)//// Modified By:// Susan Campbell, Hewlett-Packard Company (susan_campbell@hp.com)// Bapu Patil (bapu_patil@hp.com)////%////////////////////////////////////////////////////////////////////////////#include <Pegasus/Common/Config.h>#include <Pegasus/Common/Time.h>#include <Pegasus/Common/Exception.h>#include "OperatingSystem.h"#include <iostream>#include <set>#include <sys/utsname.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/param.h>#include <sys/pstat.h>#include <unistd.h>#include <netdb.h>#include <time.h>#include <utmpx.h>#include <regex.h>#include <dirent.h>#include <dl.h>/* ========================================================================== Type Definitions ========================================================================== */typedef unsigned char boolean_t;typedef unsigned long ErrorStatus_t;PEGASUS_USING_STD;OperatingSystem::OperatingSystem(void){}OperatingSystem::~OperatingSystem(void){}/** _getOSName method of the HP-UX implementation for the OS Provider Calls uname() to get the operating system name. */static Boolean _getOSName(String& osName){ struct utsname unameInfo; // Call uname and check for any errors. if ((uname(&unameInfo) < 0) && (errno != EOVERFLOW)) { return false; } osName.assign(unameInfo.sysname); return true;}/** getUtilGetHostName method for the HP-UX implementation of the OS Provider This supporting utility function gets the name of the host system from gethostname and gethostbyname. */static Boolean getUtilGetHostName(String& csName){ char hostName[PEGASUS_MAXHOSTNAMELEN + 1]; struct hostent *he; if (gethostname(hostName, sizeof(hostName)) != 0) { return false; } hostName[sizeof(hostName)-1] = 0; // Now get the official hostname. If this call fails then return // the value from gethostname(). if (he=gethostbyname(hostName)) { csName.assign(he->h_name); } else { csName.assign(hostName); } return true;}Boolean OperatingSystem::getCSName(String& csName){ return getUtilGetHostName(csName);}Boolean OperatingSystem::getName(String& osName){ return _getOSName(osName);}/** getCaption method for HP-UX implementation of OS Provider Uses uname system call and extracts information for the Caption. */Boolean OperatingSystem::getCaption(String& caption){ struct utsname unameInfo; // Call uname and check for any errors. if ((uname(&unameInfo) < 0) && (errno != EOVERFLOW)) { return false; } // append in caption the system name and release. caption.assign(unameInfo.sysname); caption.append(" "); caption.append(unameInfo.release); return true;}/** getDescription method for HP-UX implementation of OS Provider */Boolean OperatingSystem::getDescription(String& description){// 01-jul-05: Implement getDescription for HP-UX. return false;}/** getInstallDate method for HP-UX implementation of OS provider Need to determine reliable method of knowing install date one possibility is date of /stand/vmunix (but not always truly the date the OS was installed. For now, don't return any date (function returns FALSE).*/Boolean OperatingSystem::getInstallDate(CIMDateTime& installDate){// ATTN-SLC-P2-17-Apr-02: Implement getInstallDate for HP-UX return false;}/** getStatus method for HP-UX implementation of OS provider Would like to be able to return and actual status vs. just always Unknown, but didn't know how to differentiate between OK and Degraded (assuming they are the only values that make sense, since the CIMOM is up and running), but one could see an argument for including Stopping if the Shutdown or Reboot methods have been invoked. For now, always return "Unknown".*/Boolean OperatingSystem::getStatus(String& status){// ATTN-SLC-P3-17-Apr-02: Get true HP-UX status (vs. Unknown) BZ#44 status.assign("Unknown"); return true;}/** getVersion method for HP-UX implementation of OS provider Uses uname system call and extracts the release information (e.g., B.11.20). Version field in uname actually contains user license info (thus isn't included). Returns FALSE if uname call results in errors. */Boolean OperatingSystem::getVersion(String& osVersion){ struct utsname unameInfo; // Call uname and check for any errors. if ((uname(&unameInfo) < 0) && (errno != EOVERFLOW)) { return false; } osVersion.assign(unameInfo.release); return true;}/** getOSType method for HP-UX implementation of OS Provider Always returns 8 = HP-UX */Boolean OperatingSystem::getOSType(Uint16& osType){ osType = HP_UX; // from enum in .h file return true;}/** getOtherTypeDescription method for HP-UX implementation of OS provider Returns FALSE since not needed for HP-UX (don't return the empty string). */Boolean OperatingSystem::getOtherTypeDescription(String& otherTypeDescription){ return false;}/** getLastBootUpTime method for HP-UX implementation of OS Provider Gets information from pstat call. Internally in UTC (Universal Time Code) which must be converted to localtime for CIM */Boolean OperatingSystem::getLastBootUpTime(CIMDateTime& lastBootUpTime){ struct tm tmval; struct pst_static pst; char dateTimeBuffer[26]; int tzMinutesEast; // Get the static information from the pstat call to the system. if (pstat_getstatic(&pst, sizeof(pst), (size_t)1, 0) == -1) { return false; } // Get the boot time and convert to local time.// ATTN-SLC-P2-17-Apr-02: use CIMOM DateTime function & consistency, BZ#45 time_t tmp_time = pst.boot_time; localtime_r(&tmp_time, &tmval); tzMinutesEast = - (int) timezone / 60; if ((tmval.tm_isdst > 0) && daylight) { // ATTN: It is unclear how to determine the DST offset. Assume 1 hour. tzMinutesEast += 60; } // Format the date. sprintf( dateTimeBuffer, "%04d%02d%02d%02d%02d%02d.%06d%+04d", 1900 + tmval.tm_year, tmval.tm_mon + 1, // HP-UX stores month 0-11 tmval.tm_mday, tmval.tm_hour, tmval.tm_min, tmval.tm_sec, 0, tzMinutesEast); lastBootUpTime.set(dateTimeBuffer); return true;}/** getLocalDateTime method for HP-UX implementation of OS Provider Gets information from localtime call, converted to CIM DateTime format. */Boolean OperatingSystem::getLocalDateTime(CIMDateTime& localDateTime){ // Get the date and time from the system. localDateTime = CIMDateTime::getCurrentDateTime(); return true;}/** getCurrentTimeZone method for HP-UX implementation of OS Provider Gets information from Time::gettimeofday call and ensures sign follows CIM standard. */Boolean OperatingSystem::getCurrentTimeZone(Sint16& currentTimeZone){ time_t systemTime; struct tm tmval; // Get the time from the system. systemTime = time(0); localtime_r(&systemTime, &tmval); currentTimeZone = - (Sint16) (timezone / 60); if ((tmval.tm_isdst > 0) && daylight) { // ATTN: It is unclear how to determine the DST offset. Assume 1 hour. currentTimeZone += 60; } return true;}/** getNumberOfLicensedUsers method for HP-UX implementation of OS provider Calls uname and checks the version string (to get user license information. This version field doesn't currently distinguish between 128, 256, and unlimited user licensed (all = U). Need to determine how to differentiate and fix this, for now return 0 (which is unlimited). Don't know if uname -l has same limitation. */Boolean OperatingSystem::getNumberOfLicensedUsers(Uint32& numberOfLicensedUsers){ struct utsname unameInfo; // Call uname and check for any errors. if ((uname(&unameInfo) < 0) && (errno != EOVERFLOW)) { return false; } // For HP-UX, the number of licensed users is returned in the version // field of uname result. switch (unameInfo.version[0]) { case 'A' : { numberOfLicensedUsers = 2; break; } case 'B' : { numberOfLicensedUsers = 16; break; } case 'C' : { numberOfLicensedUsers = 32; break; } case 'D' : { numberOfLicensedUsers = 64; break; } case 'E' : { numberOfLicensedUsers = 8; break; } case 'U' : { // U could be 128, 256, or unlimited // need to find test system with 128 or 256 user license // to determine if uname -l has correct value // for now, return 0 = unlimited//ATTN-SLC-P2-18-Apr-02: Distinguish HP-UX 128,256,unliminted licenses BZ#43 numberOfLicensedUsers = 0; break; } default : return false; } return true;}/** getNumberOfUsers method for HP-UX implementation of OS Provider Goes through the utents, counting the number of type USER_PROCESS */Boolean OperatingSystem::getNumberOfUsers(Uint32& numberOfUsers){ struct utmpx * utmpp; numberOfUsers = 0;// ATTN-SLC-P3-17-Apr-02: optimization? parse uptime instead? while ((utmpp = getutxent()) != NULL) { if (utmpp->ut_type == USER_PROCESS) { numberOfUsers++; } } endutxent(); return true;}/** getNumberOfProcesses method for HP-UX implementation of OS Provider Gets number of active processes from pstat. */Boolean OperatingSystem::getNumberOfProcesses(Uint32& numberOfProcesses){ struct pst_dynamic psd; if (pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) == -1) { return false; } numberOfProcesses = psd.psd_activeprocs; return true;}/** getMaxNumberOfProcesses method for HP-UX implementation of OS Provider Gets maximum number of processes from pstat. */Boolean OperatingSystem::getMaxNumberOfProcesses(Uint32& mMaxProcesses){ struct pst_static pst; if (pstat_getstatic(&pst, sizeof(pst), (size_t)1, 0) == -1) { return false; } mMaxProcesses = pst.max_proc; return true;}/** _totalVM method for HP-UX implementation of HP-UX Gets information from swapinfo -q command (already in KB). Invoked for TotalVirtualMemory as well as TotalSwapSpaceSize. Would be more efficient to get this only once. */Uint64 OperatingSystem::_totalVM(){ char mline[80]; FILE * mswapInfo; Uint32 swapSize; // Initialize the return parameter in case swapinfo is not available. swapSize = 0; // Use a pipe to invoke swapinfo. if ((mswapInfo = popen("/usr/sbin/swapinfo -q 2>/dev/null", "r")) != NULL) { // Now extract the total swap space size from the swapinfo output. while (fgets(mline, 80, mswapInfo)) { sscanf(mline, "%d", &swapSize); } // end while (void)pclose (mswapInfo); } return swapSize;}/** getTotalSwapSpaceSize method for HP-UX implementation of HP-UX Gets information from swapinfo -q command (techically not swap space, it's paging). No formal paging files, report as swap. */Boolean OperatingSystem::getTotalSwapSpaceSize(Uint64& mTotalSwapSpaceSize){ mTotalSwapSpaceSize = _totalVM(); if (mTotalSwapSpaceSize != 0) return true; else return false;}/** getTotalVirutalMemorySize method for HP-UX implementation of HP-UX Gets information from swapinfo -q command (techically not swap space, it's paging). Same as the information returned for TotalSwapSpace. */Boolean OperatingSystem::getTotalVirtualMemorySize(Uint64& total){// Returns the same information as TotalSwapSpace (since the same) total = _totalVM(); if (total != 0)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?