def testLoggerLevels(self): """ _testLoggerLevels_ Just test logger levels and if correct message is printed """ logging = Logger() for level in range(len(logging.LEVELS)): testString = "Test logging level" logging.setLogLevel(level) logging.log(level, testString)
def main(): """ Main function """ logger = Logger(Logger.DEBUG) pi = ProcInfo(logger) print("first update") pi.update() print("Sleeping to accumulate") time.sleep(1) pi.update() print("System Monitoring:") sysCpuParams = ['cpu_usr', 'cpu_sys', 'cpu_idle', 'cpu_nice', 'cpu_usage', 'context_switches', 'interrupts'] sysIoParams = ['blocks_in', 'blocks_out', 'swap_in', 'swap_out'] sysMemParams = ['mem_used', 'mem_free', 'total_mem', 'mem_usage'] sysSwapParams = ['swap_used', 'swap_free', 'total_swap', 'swap_usage'] sysLoadParams = ['load1', 'load5', 'load15', 'processes', 'uptime'] sysGenParams = ['hostname', 'cpu_MHz', 'no_CPUs', 'cpu_vendor_id', 'cpu_family', 'cpu_model', 'cpu_model_name', 'bogomips'] sysNetParams = ['net_in', 'net_out', 'net_errs', 'ip'] sysNetStat = ['sockets_tcp', 'sockets_udp', 'sockets_unix', 'sockets_icm'] sysTcpDetails = ['sockets_tcp_ESTABLISHED', 'sockets_tcp_SYN_SENT', 'sockets_tcp_SYN_RECV', 'sockets_tcp_FIN_WAIT1', 'sockets_tcp_FIN_WAIT2', 'sockets_tcp_TIME_WAIT', 'sockets_tcp_CLOSED', 'sockets_tcp_CLOSE_WAIT', 'sockets_tcp_LAST_ACK', 'sockets_tcp_LISTEN', 'sockets_tcp_CLOSING', 'sockets_tcp_UNKNOWN'] print("sys_cpu_params", pi.getSystemData(sysCpuParams)) print("sys_io_params", pi.getSystemData(sysIoParams)) print("sys_mem_params", pi.getSystemData(sysMemParams)) print("sys_swap_params", pi.getSystemData(sysSwapParams)) print("sys_load_params", pi.getSystemData(sysLoadParams)) print("sys_gen_params", pi.getSystemData(sysGenParams)) print("sys_net_params", pi.getSystemData(sysNetParams)) print("sys_net_stat", pi.getSystemData(sysNetStat)) print("sys_tcp_details", pi.getSystemData(sysTcpDetails)) jobPid = os.getpid() print("Job (mysefl) monitoring:") pi.addJobToMonitor(jobPid, os.getcwd()) print("Sleep another second") time.sleep(1) pi.update() jobCpuParams = ['run_time', 'cpu_time', 'cpu_usage'] jobMemParams = ['mem_usage', 'rss', 'virtualmem', 'open_files'] jobDiskParams = ['workdir_size', 'disk_used', 'disk_free', 'disk_total', 'disk_usage'] time.sleep(10) print("job_cpu_params", pi.getJobData(jobPid, jobCpuParams)) print("job_mem_params", pi.getJobData(jobPid, jobMemParams)) print("job_disk_params", pi.getJobData(jobPid, jobDiskParams)) pi.removeJobToMonitor(os.getpid())
def __init__(self, initValue, defaultLogLevel=Logger.INFO): """ Class constructor: - if initValue is a string, put it in configAddresses and load destinations from the file named like that. if it starts with "http://", the configuration is loaded from that URL. For background monitoring, given parameters will overwrite defaults - if initValue is a list, put its contents in configAddresses and create the list of destinations from all those sources. For background monitoring, given parameters will overwrite defaults(see __defaultOptions) - if initValue is a tuple(of strings), initialize destinations with that values. Strings in this tuple have this form: "{hostname|ip}[:port][ passwd]", the default port being 8884 and the default password being "". Background monitoring will be enabled sending the parameters active from __defaultOptions(see end of file) - if initValue is a hash(key = string(hostname|ip[:port][ passwd]), val = hash{'param_name': True/False, ...}) the given options for each destination will overwrite the default parameters(see __defaultOptions) """ self.destinations = { } # empty, by default; key = tuple(host, port, pass) ; val = hash {"param_mame" : True/False, ...} self.destPrevData = { } # empty, by defaul; key = tuple(host, port, pass) ; val = hash {"param_mame" : value, ...} self.senderRef = { } # key = tuple(host, port, pass); val = hash {'INSTANCE_ID', 'SEQ_NR' } self.configAddresses = [ ] # empty, by default; list of files/urls from where we read config self.configRecheckInterval = 600 # 10 minutes self.configRecheck = True # enabled by default self.performBgMonitoring = True # by default, perform background monitoring self.monitoredJobs = {} # Monitored jobs; key = pid; value = hash with self.maxMsgRate = 100 # Maximum number of messages allowed to be sent per second self.maxMsgSize = 1440 # Maximum size of a message. Bulk parameters are split in several messages of smaller size self.__defaultSenderRef = { 'INSTANCE_ID': self.__getInstanceID(), 'SEQ_NR': 0 } self.__defaultUserCluster = "ApMon_UserSend" self.__defaultUserNode = socket.getfqdn() self.__defaultSysMonCluster = "ApMon_SysMon" self.__defaultSysMonNode = socket.getfqdn() # don't touch these: self.__freed = False self.__udpSocket = None self.__configUpdateLock = threading.Lock() self.__configUpdateEvent = threading.Event() self.__configUpdateFinished = threading.Event() self.__bgMonitorLock = threading.Lock() self.__bgMonitorEvent = threading.Event() self.__bgMonitorFinished = threading.Event() # don't allow a user to send more than MAX_MSG messages per second, in average self.__crtTime = 0 self.__prvTime = 0 self.__prvSent = 0 self.__prvDrop = 0 self.__crtSent = 0 self.__crtDrop = 0 self.__hWeight = 0.95 try: self.logger = Logger(defaultLogLevel) except: self.logger = defaultLogLevel try: self.setDestinations(initValue) self.__udpSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) if len(self.configAddresses) > 0: # if there are addresses that need to be monitored, # start config checking and reloading thread th = threading.Thread(target=self.__configLoader) th.setDaemon(True) # this is a daemon thread th.start() # create the ProcInfo instance self.procInfo = ProcInfo.ProcInfo(self.logger) # self.procInfo.update() # start the background monitoring thread th = threading.Thread(target=self.__bgMonitor) th.setDaemon(True) th.start() except Exception as msg: self.logger.log(Logger.ERROR, "Error initializing ApMon " + str(msg), True)