Ejemplo n.º 1
0
def promQueries(startTime, stopTime, testDirPath):
    prom = Prometheus()
    
    cpu5s = json.loads(prom.query_rang(metric='sum(container_cpu_usage_seconds_total{namespace="default"}) by (pod_name)', start=startTime, end=stopTime, step='5s'))
    memWriteB5s = json.loads(prom.query_rang(metric='sum(container_fs_writes_bytes_total{namespace="default"}) by (pod_name)', start=startTime, end=stopTime, step='5s'))
    memReadB5s = json.loads(prom.query_rang(metric='sum(container_fs_reads_bytes_total{namespace="default"}) by (pod_name)', start=startTime, end=stopTime, step='5s'))
    netReadB5s = json.loads(prom.query_rang(metric='sum(container_network_receive_bytes_total{namespace="default"}) by (pod_name)', start=startTime, end=stopTime, step='5s'))
    netWriteB5s = json.loads(prom.query_rang(metric='sum(container_network_transmit_bytes_total{namespace="default"}) by (pod_name)', start=startTime, end=stopTime, step='5s'))

    """ cpu5s = json.loads(prom.query_rang(metric='sum(container_cpu_usage_seconds_total{namespace="robot-shop"}) by (pod_name)', start=startTime, end=stopTime, step='5s'))
    memWriteB5s = json.loads(prom.query_rang(metric='sum(container_fs_writes_bytes_total{namespace="robot-shop"}) by (pod_name)', start=startTime, end=stopTime, step='5s'))
    memReadB5s = json.loads(prom.query_rang(metric='sum(container_fs_reads_bytes_total{namespace="robot-shop"}) by (pod_name)', start=startTime, end=stopTime, step='5s'))
    netReadB5s = json.loads(prom.query_rang(metric='sum(container_network_receive_bytes_total{namespace="robot-shop"}) by (pod_name)', start=startTime, end=stopTime, step='5s'))
    netWriteB5s = json.loads(prom.query_rang(metric='sum(container_network_transmit_bytes_total{namespace="robot-shop"}) by (pod_name)', start=startTime, end=stopTime, step='5s')) """
    
    # Can use queries below to find rate of change also
    """  cpuAvg = json.loads(prom.query_rang(metric='sum(rate(container_cpu_usage_seconds_total{namespace="robot-shop"}[1m])) by (pod_name)', start=startTime, end=stopTime, step='5s'))
    memWriteBavg = json.loads(prom.query_rang(metric='sum(rate(container_fs_writes_bytes_total{namespace="robot-shop"}[1m])) by (pod_name)', start=startTime, end=stopTime, step='5s'))
    memReadBavg = json.loads(prom.query_rang(metric='sum(rate(container_fs_reads_bytes_total{namespace="robot-shop"}[1m])) by (pod_name)', start=startTime, end=stopTime, step='5s'))
    netReadBavg = json.loads(prom.query_rang(metric='sum(rate(container_network_receive_bytes_total{namespace="robot-shop"}[1m])) by (pod_name)', start=startTime, end=stopTime, step='5s'))
    netWriteBavg = json.loads(prom.query_rang(metric='sum(rate(container_network_transmit_bytes_total{namespace="robot-shop"}[1m])) by (pod_name)', start=startTime, end=stopTime, step='5s')) """

    podMetricsDict = {} # List of podDataCollection objects
    timestampList = [] # List of scraped timestamps
    podNameList = [] # List of scraped pods

    # Create list of podDataCollection objects, with CPU vals:
    for pod in cpu5s['data']['result']:
        p = podDataCollection(pod['metric']['pod_name'])
        podNameList.append(pod['metric']['pod_name'])
        p.cpu5s = pod['values']
        podMetricsDict[p.podName] = p

    for tStamp, val in pod['values']:
        timestampList.append(tStamp)

    for pod in memWriteB5s['data']['result']:
        podMetricsDict[pod['metric']['pod_name']].memW5s = pod['values']
        
    for pod in memReadB5s['data']['result']:
        podMetricsDict[pod['metric']['pod_name']].memR5s = pod['values']  

    for pod in netWriteB5s['data']['result']:
        podMetricsDict[pod['metric']['pod_name']].netW5s = pod['values']   

    for pod in netReadB5s['data']['result']:
        podMetricsDict[pod['metric']['pod_name']].netR5s = pod['values']

    createRawCSVs(timestampList, podNameList, testDirPath, podMetricsDict)
Ejemplo n.º 2
0
def send_query(query, start, end, step, url):
    prometheus = Prometheus()
    prometheus.url = url

    res = prometheus.query_rang(metric=query, start=start, end=end, step=step)
    return res
from prometheus_http_client import Prometheus 
import prometheus_http_client
from prometheus_http_client.prometheus import *
import os

os.environ['PROMETHEUS_URL'] = 'http://demo.robustperception.io:9090/'
@prom
def up(*args, **kwargs):
    pass
@relabel('100 - (avg by (instance, job) (irate(node_cpu{mode="idle"}[5m])) * 100)')
def hello(*args,**kwargs):
    pass
if __name__ == '__main__':
    print("Up metrics ::",up())
    print("Node CPU Metrics :",hello())
    print("UP With Range ::")
    p = Prometheus()
    print(p.query_rang(metric="up", start=1570221677, end=1570225677))
    print(p.label_values('job'))
    print(dir(Prometheus))
Ejemplo n.º 4
0
def promQueries(startTime, stopTime, testDirPath):
    prom = Prometheus()
    namespace = "robot-shop"
    step = '5s'
    # If you’re using Kubernetes 1.16 and above you’ll have to use pod instead of pod_name and container instead of container_name.
    # Can use queries below to find rate of change also
    cpu5s = json.loads(
        prom.query_rang(
            metric='sum(container_cpu_usage_seconds_total{namespace="' +
            namespace + '"}) by (pod)',
            start=startTime,
            end=stopTime,
            step=step))
    memWriteB5s = json.loads(
        prom.query_rang(
            metric='sum(container_fs_writes_bytes_total{namespace="' +
            namespace + '"}) by (pod)',
            start=startTime,
            end=stopTime,
            step=step))
    memReadB5s = json.loads(
        prom.query_rang(
            metric='sum(container_fs_reads_bytes_total{namespace="' +
            namespace + '"}) by (pod)',
            start=startTime,
            end=stopTime,
            step=step))
    netReadB5s = json.loads(
        prom.query_rang(
            metric='sum(container_network_receive_bytes_total{namespace="' +
            namespace + '"}) by (pod)',
            start=startTime,
            end=stopTime,
            step=step))
    netWriteB5s = json.loads(
        prom.query_rang(
            metric='sum(container_network_transmit_bytes_total{namespace="' +
            namespace + '"}) by (pod)',
            start=startTime,
            end=stopTime,
            step=step))

    #cpu5s = json.loads(prom.query_rang(metric='sum(irate(container_cpu_usage_seconds_total{namespace="'+namespace+'"}[1m])) by (pod)', start=startTime, end=stopTime, step=step))
    #memWriteB5s = json.loads(prom.query_rang(metric='sum(rate(container_fs_writes_bytes_total{namespace="'+namespace+'"}[1m])) by (pod)', start=startTime, end=stopTime, step=step))
    #memReadB5s = json.loads(prom.query_rang(metric='sum(rate(container_fs_reads_bytes_total{namespace="'+namespace+'"}[1m])) by (pod)', start=startTime, end=stopTime, step=step))
    #netReadB5s = json.loads(prom.query_rang(metric='sum(irate(container_network_receive_bytes_total{namespace="'+namespace+'"}[1m])) by (pod)', start=startTime, end=stopTime, step=step))
    #netWriteB5s = json.loads(prom.query_rang(metric='sum(irate(container_network_transmit_bytes_total{namespace="'+namespace+'"}[1m])) by (pod)', start=startTime, end=stopTime, step=step))

    podMetricsDict = {}  # List of podDataCollection objects
    timestampList = []  # List of scraped timestamps
    podNameList = []  # List of scraped pods
    tmp_pod = []
    print("cpu5s, ", cpu5s)
    # Create list of podDataCollection objects, with CPU vals:
    for pod in cpu5s['data']['result']:
        p = podDataCollection(pod['metric']['pod'])
        podNameList.append(pod['metric']['pod'])
        p.cpu5s = pod['values']
        podMetricsDict[p.podName] = p
        if not tmp_pod:
            tmp_pod = pod['values']
    #print("tmp_pdo", tmp_pod)
    for tStamp, val in tmp_pod:
        timestampList.append(tStamp)

    for pod in memWriteB5s['data']['result']:
        podMetricsDict[pod['metric']['pod']].memW5s = pod['values']

    for pod in memReadB5s['data']['result']:
        podMetricsDict[pod['metric']['pod']].memR5s = pod['values']

    for pod in netWriteB5s['data']['result']:
        podMetricsDict[pod['metric']['pod']].netW5s = pod['values']

    for pod in netReadB5s['data']['result']:
        podMetricsDict[pod['metric']['pod']].netR5s = pod['values']
    #print(podMetricsDict)

    createRawCSVs(timestampList, podNameList, testDirPath, podMetricsDict)