Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify procdockerstatsd to update the redis STATE_DB with system disk… #168

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions scripts/procdockerstatsd
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ class ProcDockerStats(daemon_base.DaemonBase):
process_data_list.append(process_data)
return process_data_list

def format_mount_cmd_output(self, cmdout):
lines = cmdout.splitlines()
keys = re.split(" +", lines[0])
mount_data = dict()
mount_data_list = []
for line in lines[1:]:
values = line.split()
mount_data_list.append(values)
formatted_dict = self.create_mount_dict(mount_data_list)
return formatted_dict

def convert_to_bytes(self, value):
UNITS_B = 'B'
UNITS_KB = 'KB'
Expand Down Expand Up @@ -120,6 +131,20 @@ class ProcDockerStats(daemon_base.DaemonBase):
dockerdict[key]['PIDS'] = row.get('PIDS')
return dockerdict

def create_mount_dict(self, dict_list):
mountdict = {}
for row in dict_list[0:]:
if row[1] == "tmpfs" or row[1] == "devtmpfs" or row[1] == "overlay":
continue
key = 'MOUNT_POINTS|{}'.format(row[6])
mountdict[key] = {}
mountdict[key]['Filesystem'] = row[0]
mountdict[key]['Type'] = row[1]
mountdict[key]['1K-blocks'] = row[2]
mountdict[key]['Used'] = row[3]
mountdict[key]['Available'] = row[4]
return mountdict

def update_dockerstats_command(self):
cmd = ["docker", "stats", "--no-stream", "-a"]
data = self.run_command(cmd)
Expand Down Expand Up @@ -223,6 +248,23 @@ class ProcDockerStats(daemon_base.DaemonBase):
update_value['enabled'] = str(enabled)
self.batch_update_state_db(fips_db_key, update_value)

def update_mountpointstats_command(self):
cmd = ["df", "-T"]
data = self.run_command(cmd)
if not data:
self.log_error("'{}' returned null output for filesystem".format(cmd))
return False
mountdata = self.format_mount_cmd_output(data)
if not mountdata:
self.log_error("formatting for filesystem output failed")
return False
self.state_db.delete_all_by_pattern('STATE_DB', 'MOUNT_POINTS|*')
for k1, v1 in mountdata.items():
for k2, v2 in v1.items():
self.update_state_db(k1, k2, v2)
return True


def update_state_db(self, key1, key2, value2):
self.state_db.set('STATE_DB', key1, key2, value2)

Expand All @@ -237,6 +279,7 @@ class ProcDockerStats(daemon_base.DaemonBase):
print("Must be root to run this daemon")
sys.exit(1)

counter = 1
while True:
self.update_dockerstats_command()
datetimeobj = datetime.now()
Expand All @@ -247,9 +290,17 @@ class ProcDockerStats(daemon_base.DaemonBase):
self.update_fipsstats_command()
self.update_state_db('FIPS_STATS|LastUpdateTime', 'lastupdate', str(datetimeobj))

if counter == 5:
self.update_mountpointstats_command()
self.update_state_db('MOUNT_POINTS|LastUpdateTime', 'lastupdate', str(datetimeobj))
counter = 1

# Data need to be updated every 2 mins. hence adding delay of 120 seconds
time.sleep(120)

# Adding a counter to ensure mount points only updates every 10 mins, instead of 2 mins
counter += 1

self.log_info("Exiting ...")


Expand Down
Loading