Skip to content

Commit

Permalink
Update prometheus.py
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxarun authored May 28, 2022
1 parent 762d5ed commit 3ab93fa
Showing 1 changed file with 46 additions and 34 deletions.
80 changes: 46 additions & 34 deletions prometheus.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@

service = service = os.uname()[1]

def main():
gather_metrics()

metrics_data_map = {}

metrics_file = "/root/prometheus/data/metrics_data"
log_file = "/var/log/application_metrics.log"
metrics_file_path = "/root/prometheus/data/"
metrics_file = metrics_file_path + "metrics_data"

log_file_path = "/appl/log/metrics/"
log_file = log_file_path + "application_metrics.log"

def main():
gather_metrics()

def prometheus():
f = open(metrics_file,"r")
Expand Down Expand Up @@ -66,6 +69,9 @@ def write_metrics():
for item in data_value[0]:
metric_str += item.split('=')[0] + "=" + "\"" + item.split('=')[1] + "\"" + ","
metric_str += "} " + str(data_value[2]) + "\n"

if not os.path.exists(metrics_file_path):
os.makedirs(metrics_file_path)
f = open(metrics_file,"w")
f.write(metric_str)
f.close()
Expand Down Expand Up @@ -108,36 +114,42 @@ def gather_metrics():


def tail_log_file():
if not os.path.exists(log_file):
open(log_file, 'w').close()
os.chmod(log_file, 0o777)
current = open(log_file, "r")
curino = os.fstat(current.fileno()).st_ino
current.seek(0, os.SEEK_END)
while True:
line = current.readline()
if not line:
sleep(0.1)
try:
if oct(os.stat(log_file).st_mode)[-3:] != '777':
os.chmod(log_file, 0o777)
if os.stat(log_file).st_ino != curino:
new = open(log_file, "r")
current.close()
current = new
curino = os.fstat(current.fileno()).st_ino
continue
except IOError:
if not os.path.exists(log_file):
open(log_file, 'w').close()
os.chmod(log_file, 0o777)
current = open(log_file, "r")
curino = os.fstat(current.fileno()).st_ino
pass
else:
line.replace('\n','')
line.replace('\r\n','')
yield line
try:
if not os.path.exists(log_file_path):
os.makedirs(log_file_path)
if not os.path.exists(log_file):
open(log_file, 'w').close()
os.chmod(log_file, 0o777)
current = open(log_file, "r")
curino = os.fstat(current.fileno()).st_ino
current.seek(0, os.SEEK_END)
while True:
line = current.readline()
if not line:
sleep(0.1)
try:
if oct(os.stat(log_file).st_mode)[-3:] != '777':
os.chmod(log_file, 0o777)
if os.stat(log_file).st_ino != curino:
new = open(log_file, "r")
current.close()
current = new
curino = os.fstat(current.fileno()).st_ino
continue
except IOError:
if not os.path.exists(log_file):
open(log_file, 'w').close()
os.chmod(log_file, 0o777)
current = open(log_file, "r")
curino = os.fstat(current.fileno()).st_ino
pass
else:
line.replace('\n','')
line.replace('\r\n','')
yield line
except IOError:
pass

if __name__ == "__main__":
main()

0 comments on commit 3ab93fa

Please sign in to comment.