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:
indicator_data = db.session.query(Indicator_data.indicator_data_id,
Indicator_data.indicator_id, Indicator_data.value, Vnfc_instance.vnfc_instance_id,
Vnfc_instance.vnfc_instance_name,
func.max(Indicator_data.timestamp)).group_by(Indicator_data.indicator_id)
get_indicators_list = []
for indicator_record in indicator_data:
indicator_data_id = indicator_record.indicator_data_id
value = indicator_record.value
vnfc_instance_id = indicator_record.vnfc_instance_id
vnfc_instance_name = indicator_record.vnfc_instance_name
self_dict = {
'href': request.base_url ,
}
vnfInstance_dict = {
'href': 'https//:www.example.com',
}
links_dic = {
'self': self_dict,
'vnfInstance': vnfInstance_dict
}
get_indicators_dict = {
'_links': links_dic,
'id': indicator_data_id,
'value': value,
'vnfInstanceId': vnfc_instance_id,
'name': vnfc_instance_name
}
get_indicators_list.append(get_indicators_dict)
print(get_indicators_dict)
return get_indicators_list
Advertisement
Answer
See this example:
import psutil
import time
from prometheus.collectors import Gauge
ram_metric = Gauge("memory_usage_bytes", "Memory usage in bytes.",
{'host': host})
cpu_metric = Gauge("cpu_usage_percent", "CPU usage percent.",
{'host': host})
while True:
time.sleep(1)
# Add ram metrics
ram = psutil.virtual_memory()
swap = psutil.swap_memory()
ram_metric.set({'type': "virtual", }, ram.used)
ram_metric.set({'type': "virtual", 'status': "cached"}, ram.cached)
ram_metric.set({'type': "swap"}, swap.used)
# Add cpu metrics
for c, p in enumerate(psutil.cpu_percent(interval=1, percpu=True)):
cpu_metric.set({'core': c}, p)