[procdockerstatsd] Inherit DaemonBase class from sonic-py-common package (#5372)
Eliminate duplicate logging code by inheriting from DaemonBase class in sonic-py-common package.
This commit is contained in:
parent
353003f6ee
commit
3a901eeae0
@ -8,10 +8,10 @@ import os
|
|||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import syslog
|
|
||||||
import time
|
import time
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
from sonic_py_common import daemon_base
|
||||||
import swsssdk
|
import swsssdk
|
||||||
|
|
||||||
VERSION = '1.0'
|
VERSION = '1.0'
|
||||||
@ -20,26 +20,11 @@ SYSLOG_IDENTIFIER = "procdockerstatsd"
|
|||||||
|
|
||||||
REDIS_HOSTIP = "127.0.0.1"
|
REDIS_HOSTIP = "127.0.0.1"
|
||||||
|
|
||||||
# ========================== Syslog wrappers ==========================
|
|
||||||
def log_info(msg, also_print_to_console=False):
|
|
||||||
syslog.openlog(SYSLOG_IDENTIFIER)
|
|
||||||
syslog.syslog(syslog.LOG_INFO, msg)
|
|
||||||
syslog.closelog()
|
|
||||||
|
|
||||||
def log_warning(msg, also_print_to_console=False):
|
class ProcDockerStats(daemon_base.DaemonBase):
|
||||||
syslog.openlog(SYSLOG_IDENTIFIER)
|
|
||||||
syslog.syslog(syslog.LOG_WARNING, msg)
|
|
||||||
syslog.closelog()
|
|
||||||
|
|
||||||
def log_error(msg, also_print_to_console=False):
|
def __init__(self, log_identifier):
|
||||||
syslog.openlog(SYSLOG_IDENTIFIER)
|
super(ProcDockerStats, self).__init__(log_identifier)
|
||||||
syslog.syslog(syslog.LOG_ERR, msg)
|
|
||||||
syslog.closelog()
|
|
||||||
|
|
||||||
# ========================== ProcessDocker class ==========================
|
|
||||||
class ProcDockerStats:
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self.state_db = swsssdk.SonicV2Connector(host=REDIS_HOSTIP)
|
self.state_db = swsssdk.SonicV2Connector(host=REDIS_HOSTIP)
|
||||||
self.state_db.connect("STATE_DB")
|
self.state_db.connect("STATE_DB")
|
||||||
|
|
||||||
@ -47,7 +32,7 @@ class ProcDockerStats:
|
|||||||
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
|
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
|
||||||
(stdout, stderr) = proc.communicate()
|
(stdout, stderr) = proc.communicate()
|
||||||
if proc.returncode != 0:
|
if proc.returncode != 0:
|
||||||
log_error("Error running command '{}'".format(cmd))
|
self.log_error("Error running command '{}'".format(cmd))
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
return stdout
|
return stdout
|
||||||
@ -144,11 +129,11 @@ class ProcDockerStats:
|
|||||||
cmd = "docker stats --no-stream -a"
|
cmd = "docker stats --no-stream -a"
|
||||||
data = self.run_command(cmd)
|
data = self.run_command(cmd)
|
||||||
if not data:
|
if not data:
|
||||||
log_error("'{}' returned null output".format(cmd))
|
self.log_error("'{}' returned null output".format(cmd))
|
||||||
return False
|
return False
|
||||||
dockerdata = self.format_docker_cmd_output(data)
|
dockerdata = self.format_docker_cmd_output(data)
|
||||||
if not dockerdata:
|
if not dockerdata:
|
||||||
log_error("formatting for docker output failed")
|
self.log_error("formatting for docker output failed")
|
||||||
return False
|
return False
|
||||||
# wipe out all data from state_db before updating
|
# wipe out all data from state_db before updating
|
||||||
self.state_db.delete_all_by_pattern('STATE_DB', 'DOCKER_STATS|*')
|
self.state_db.delete_all_by_pattern('STATE_DB', 'DOCKER_STATS|*')
|
||||||
@ -188,26 +173,36 @@ class ProcDockerStats:
|
|||||||
self.state_db.set('STATE_DB', key1, key2, value2)
|
self.state_db.set('STATE_DB', key1, key2, value2)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.update_dockerstats_command()
|
self.log_info("Starting up ...")
|
||||||
datetimeobj = datetime.now()
|
|
||||||
# Adding key to store latest update time.
|
if not os.getuid() == 0:
|
||||||
self.update_state_db('DOCKER_STATS|LastUpdateTime', 'lastupdate', datetimeobj)
|
self.log_error("Must be root to run this daemon")
|
||||||
self.update_processstats_command()
|
print("Must be root to run this daemon")
|
||||||
self.update_state_db('PROCESS_STATS|LastUpdateTime', 'lastupdate', datetimeobj)
|
sys.exit(1)
|
||||||
|
|
||||||
|
while True:
|
||||||
|
self.update_dockerstats_command()
|
||||||
|
datetimeobj = datetime.now()
|
||||||
|
# Adding key to store latest update time.
|
||||||
|
self.update_state_db('DOCKER_STATS|LastUpdateTime', 'lastupdate', datetimeobj)
|
||||||
|
self.update_processstats_command()
|
||||||
|
self.update_state_db('PROCESS_STATS|LastUpdateTime', 'lastupdate', datetimeobj)
|
||||||
|
|
||||||
|
# Data need to be updated every 2 mins. hence adding delay of 120 seconds
|
||||||
|
time.sleep(120)
|
||||||
|
|
||||||
|
self.log_info("Exiting ...")
|
||||||
|
|
||||||
|
|
||||||
# main start
|
|
||||||
def main():
|
def main():
|
||||||
log_info("process-docker stats daemon starting up..")
|
# Instantiate a ProcDockerStats object
|
||||||
if not os.getuid() == 0:
|
pd = ProcDockerStats(SYSLOG_IDENTIFIER)
|
||||||
log_error("Must be root to run process-docker daemon")
|
|
||||||
print "Error: Must be root to run process-docker daemon"
|
# Log all messages from INFO level and higher
|
||||||
sys.exit(1)
|
pd.set_min_log_priority_info()
|
||||||
pd = ProcDockerStats()
|
|
||||||
# Data need to be updated every 2 mins. hence adding delay of 120 seconds
|
pd.run()
|
||||||
while True:
|
|
||||||
pd.run()
|
|
||||||
time.sleep(120)
|
|
||||||
log_info("process-docker stats daemon exited")
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
Loading…
Reference in New Issue
Block a user