【问题标题】:How to create a configurable python script to extract data for graphite?如何创建可配置的 Python 脚本来提取石墨数据?
【发布时间】:2017-11-30 22:46:50
【问题描述】:

我有一个在不同机器(代理、平衡器、注册器...)上运行的软件,并希望使用collectd 将统计信息发送到graphite

为此,我创建了一个 python 脚本,在该脚本中,我对一个命令的返回值进行了一些正则表达式搜索,该命令列出了属于该软件的所有统计信息。由于我想在所有不同的机器上使用相同的脚本,我需要通过设置文件以某种方式对其进行配置。这样根据设置文件,每台机器都会向石墨发送不同的参数。

我的部分脚本:

stats = softwareX.get_all_statistics()   
call_mem = stats.findall(...mem_regex...).group(1)  
call_time = stats.findall(...time_regex...).group(1)  

考虑到我从stats 中提取了许多参数,并且希望根据脚本运行的机器只发送其中的特定组。例如,call_mem 是平衡机发送给graphite 的参数之一(但不会发送call_time),而代理机器则相反(将发送call_time ,但不是call_mem)。

如何在一个可配置的 Python 脚本中为所有机器完成这项工作?

【问题讨论】:

    标签: python graphite


    【解决方案1】:

    我不知道你是否仍然需要回答这个问题,但你可以尝试这样的事情:

    collect_and_send_stats.py

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    import sys
    
    all_stats_regex_dict = {
        'call_mem': 'here goes the call_mem regex',
        'call_time': 'here goes the call_time regex',
        # ...
    }
    
    def parse_stats(stat_name_list, all_stats_of_this_software):
        parsed_stats = {}
        for stat_name in stat_name_list:
            stat_regex = all_stats_regex_dict[stat_name]
            parsed_stats[stat_name] = all_stats_of_this_software.findall(stat_regex).group(1)
    
        return parsed_stats
    
    if __name__ == '__main__':
        stat_name_list = sys.argv[1:]
    
        # somehow get the stats of this machine/software
        all_stats = get_all_statistics()
    
        stats = parse_stats(stat_name_list, all_stats)
    
        # send stats to graphite
        send(stats)
    

    然后你可以在平衡器上这样调用这个脚本:

    ./collect_and_send_stats.py call_mem
    

    在代理上也这样

    ./collect_and_send_stats.py call_time
    

    希望这能为您指明一个有用的方向(如果您还没有解决问题)。

    【讨论】:

      猜你喜欢
      • 2017-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      • 2012-06-15
      • 2021-08-24
      • 1970-01-01
      相关资源
      最近更新 更多