Created
May 2, 2021 16:27
-
-
Save Nancy-Chauhan/105d9db52a06fe37d0039cad6a037a93 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
from time import sleep | |
from prometheus_client import start_http_server, Counter | |
def main(): | |
start_http_server(8000) | |
gather_metrics() | |
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(match.group(1)) | |
request_counter.labels(status_code=status_code).inc() | |
if match.group(2) != "-": | |
size = int(match.group(2)) | |
bytes_sent.inc(size) | |
else: | |
print("line did not match", line) | |
def follow_log(file): | |
with open(file, 'r') as f: | |
f.seek(0, os.SEEK_END) | |
# infinite loop | |
while True: | |
line = f.readline() | |
if not line: | |
sleep(0.1) | |
continue | |
yield line | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment