Skip to content

Instantly share code, notes, and snippets.

Created May 2, 2021 16:27
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Nancy-Chauhan/105d9db52a06fe37d0039cad6a037a93 to your computer and use it in GitHub Desktop.
import os
import re
from time import sleep
from prometheus_client import start_http_server, Counter
def main():
def gather_metrics():
print("collecting httpd metrics")
regex = r"\d+\.\d+\.\d+\.\d+ - \S+ \[.*\] \".*?\" (\d+) (\d+|-).*"
request_counter = Counter('httpd_requests', 'http request', ["status_code"])
bytes_sent = Counter('httpd_bytes_sent', 'Total bytes sent by httpd')
for line in follow_log("/var/log/apache2/access.log"):
match = re.match(regex, line)
if match:
print("updating values")
status_code = int(
if != "-":
size = int(
print("line did not match", line)
def follow_log(file):
with open(file, 'r') as f:, os.SEEK_END)
# infinite loop
while True:
line = f.readline()
if not line:
yield line
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment