0

我正在尝试运行一些 python/其他语言模块模块/工作流/工作负载,并使用 Grahite、Carbon、CollectD、StatsD 收集它们的 CPU、Mem、I/O 等资源使用情况。我已阅读有关创建标记系列的文档(请参阅:此处),但我似乎无法找到如何标记特定模块的内容。例如,我有两个模块

第一个模块

def firstModule:
    # Initialize a list
    primes = []

    for possiblePrime in range(2, 21):
       # Assume number is prime until shown it is not. 

       isPrime = True

       for num in range(2, possiblePrime):
          if possiblePrime % num == 0:
          isPrime = False

    if isPrime:
        primes.append(possiblePrime)

第二模块

def secondModule:
    # Initialize a list

    primes = []

    for possiblePrime in range(2, 21):

        # Assume number is prime until shown it is not. 

        isPrime = True

        for num in range(2, possiblePrime):
            if possiblePrime % num == 0:
            isPrime = False
            break

    if isPrime:
        primes.append(possiblePrime)

在这里,我想调用这两个模块,然后标记资源使用指标,以便我可以将其发送到 Whisper 数据库,如下面的代码所示:我该如何实现?

firstModule() # assign some tag say A
secondModule() # assign some tag say B
4

1 回答 1

0

虽然我最终没有使用带标签的系列,但我通过编写 bash 脚本并在石墨中使用渲染 api解决了上述问题。这是解决方案:

    #!/bin/bash   

    #Remove previous records. Be careful not to remove useful files!
    rm cpu.json, mem.json

    #clear variables for storing time 
    unset start
    unset finish

    #log start time of process using absolute time, if using relative time check doc at https://graphite.readthedocs.io/en/latest/render_api.html for rules
    start=$(date '+%H:%M_%Y%m%d')

    #call first module
    firstModule()

    #sleep 1 minute or more because you will get an error that start time and end time are equal
    sleep 100

    #call second module
    secondModule()

    #log finish date using absolute time, if using relative time check  doc at https://graphite.readthedocs.io/en/latest/render_api.html) for rules
    finish=$(date '+%H:%M_%Y%m%d')

    #collect data using curl here cpu usage and memusage, adjust to fit your setup and Voila!
    curl "http://host/render?target=carbon.agents.*.cpuUsage&width=500&height=300&from={$start}&until={$finish}&format=json" > cpu.json
    sudo curl "http://host/render?target=collectdlocalhost.memory.memory-used&width=500&height=300&from={$start}&until={$finish}&format=json" > mem.json
于 2018-10-26T14:59:41.373 回答