Commit 17a1f47f authored by wolfsack's avatar wolfsack
Browse files

project

parent bc18c4f5
/venv
/.idea
/app/__pycache__
\ No newline at end of file
FROM python:3.9
ENV PYTHONUNBUFFERED 1
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
COPY /app .
EXPOSE 5000
ENTRYPOINT ["python"]
CMD ["app.py"]
\ No newline at end of file
from flask import Flask, make_response
from metrics import generate_metrics
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
@app.route('/metrics')
def metrics():
response = make_response(generate_metrics(), 200)
response.mimetype = "text/plain"
return response
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
class Metric:
def __init__(self, metric_name, metric_type, comment, value, params):
if metric_name is None or metric_type is None or value is None:
raise ValueError
self.metric_name = metric_name
self.metric_type = metric_type
self.comment = comment
self.value = value
self.params = params
def to_string(self):
help_string = f'# HELP {self.metric_name} {self.comment}'
type_string = f'# TYPE {self.metric_name} {self.metric_type}'
output = ""
if self.comment is not None:
output += help_string + "\r\n"
output = type_string + "\r\n" + f'{self.metric_name}{self.__params_to_string()} {self.value}'
return output
def __params_to_string(self):
if self.params is None:
return ""
output = "{"
for k, v in self.params.items():
output += f'{k}="{v}" ,'
return output[0:-2] + "}"
import psutil
from metric import Metric
def metrics():
cpu = psutil.cpu_percent()
cores = psutil.cpu_count()
cores_physical = psutil.cpu_count(logical=False)
memory = psutil.virtual_memory()
boot_time = psutil.boot_time()
partitions = psutil.disk_partitions()
metrics_list = [Metric(
metric_name="cpu_usage",
metric_type="gauge",
comment="CPU Usage in Percent",
value=cpu,
params=None
), Metric(
metric_name="cpu_cores",
metric_type="gauge",
comment="Total CPU Cores",
value=cores,
params={"type": "all"}
), Metric(
metric_name="cpu_cores",
metric_type="gauge",
comment="Total CPU Cores",
value=cores_physical,
params={"type": "physical"}
), Metric(
metric_name="boot_time",
metric_type="gauge",
comment="Time in ms",
value=boot_time,
params=None
), Metric(
metric_name="memory_usage",
metric_type="gauge",
comment="Memory Usage Data",
value=memory[0],
params={
"type": "total"
}
), Metric(
metric_name="memory_usage",
metric_type="gauge",
comment="Memory Usage Data",
value=memory[1],
params={
"type": "available"
}
), Metric(
metric_name="memory_usage",
metric_type="gauge",
comment="Memory Usage Data",
value=memory[3],
params={
"type": "used"
}
), Metric(
metric_name="memory_usage",
metric_type="gauge",
comment="Memory Usage Data",
value=memory[4],
params={
"type": "free"
}
)]
for partition in partitions:
disk = psutil.disk_usage(partition[1])
metrics_list.append(
Metric(
metric_name="disk_usage",
metric_type="gauge",
comment="Disk Usage Data",
value=disk[0],
params={
"mount": partition[1],
"type": "total"
}
))
metrics_list.append(
Metric(
metric_name="disk_usage",
metric_type="gauge",
comment="Disk Usage Data",
value=disk[1],
params={
"mount": partition[1],
"type": "used"
}
))
metrics_list.append(
Metric(
metric_name="disk_usage",
metric_type="gauge",
comment="Disk Usage Data",
value=disk[2],
params={
"mount": partition[1],
"type": "free"
}
))
return metrics_list
def generate_metrics():
output = ""
for metric in metrics():
output += metric.to_string() + "\r\n" + "\r\n"
return output
version: "3"
services:
pi:
build: .
image: pi:latest
container_name: pi
ports:
- "5000:5000"
restart: unless-stopped
\ No newline at end of file
click==8.0.3
colorama==0.4.4
Flask==2.0.2
itsdangerous==2.0.1
Jinja2==3.0.3
MarkupSafe==2.0.1
psutil==5.8.0
Werkzeug==2.0.2
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment