Currently I am developing a python flask application. I need to get cpu, memory, disk data of the machine. I suppose to get those data using node-exporter using Python scripts. Currently I am working with dummy data.
Here are the instructions provided:
- Select two simple metrics
- Send http call to node exporter and save coutput to a file or keep in a variable
- Extract the value of the metric
- You can have the mapping of metric and node exporter metric in a separate file
- Save the value in the table
Does anybody have any idea about how to develop the this kind of Python script?
This is my controller classes without node-exporter queries:
JavaScript
x
37
37
1
indicator_data = db.session.query(Indicator_data.indicator_data_id,
2
Indicator_data.indicator_id, Indicator_data.value, Vnfc_instance.vnfc_instance_id,
3
Vnfc_instance.vnfc_instance_name,
4
func.max(Indicator_data.timestamp)).group_by(Indicator_data.indicator_id)
5
6
7
get_indicators_list = []
8
for indicator_record in indicator_data:
9
10
indicator_data_id = indicator_record.indicator_data_id
11
value = indicator_record.value
12
vnfc_instance_id = indicator_record.vnfc_instance_id
13
vnfc_instance_name = indicator_record.vnfc_instance_name
14
15
self_dict = {
16
'href': request.base_url ,
17
}
18
vnfInstance_dict = {
19
'href': 'https//:www.example.com',
20
}
21
links_dic = {
22
'self': self_dict,
23
'vnfInstance': vnfInstance_dict
24
}
25
26
get_indicators_dict = {
27
'_links': links_dic,
28
'id': indicator_data_id,
29
'value': value,
30
'vnfInstanceId': vnfc_instance_id,
31
'name': vnfc_instance_name
32
33
}
34
get_indicators_list.append(get_indicators_dict)
35
print(get_indicators_dict)
36
return get_indicators_list
37
Advertisement
Answer
See this example:
JavaScript
1
25
25
1
import psutil
2
import time
3
4
from prometheus.collectors import Gauge
5
6
ram_metric = Gauge("memory_usage_bytes", "Memory usage in bytes.",
7
{'host': host})
8
cpu_metric = Gauge("cpu_usage_percent", "CPU usage percent.",
9
{'host': host})
10
11
while True:
12
time.sleep(1)
13
14
# Add ram metrics
15
ram = psutil.virtual_memory()
16
swap = psutil.swap_memory()
17
18
ram_metric.set({'type': "virtual", }, ram.used)
19
ram_metric.set({'type': "virtual", 'status': "cached"}, ram.cached)
20
ram_metric.set({'type': "swap"}, swap.used)
21
22
# Add cpu metrics
23
for c, p in enumerate(psutil.cpu_percent(interval=1, percpu=True)):
24
cpu_metric.set({'core': c}, p)
25