Python版本的“facter”?

4 投票
4 回答
3911 浏览
提问于 2025-04-16 12:42

我在考虑收集服务器上的数据,而这些服务器上预装的是Python 2.6。现在我想知道,是否有类似于Ruby中“facter”的Python库,而不是Python对facter的“绑定”。

我在网上搜索过,但没有找到相关的信息。有没有人知道这个问题的答案?

4 个回答

1

有点新手的朋友可以看看这个链接:http://github.com/hihellobolke/sillyfacter/

安装的方法是:

  # Needs pip v1.5
  pip install --upgrade --allow-all-external --allow-unverified netifaces sillyfacter

你可能还需要升级一下pip。

  # To upgrade pip
  pip install --ugrade pip
5

Salt有一个叫做Grains的功能,它是用来替代Facter的。

http://docs.saltstack.org/en/latest/ref/modules/index.html#grains-data

还有一个类似的尝试,叫做Phacter。

http://code.google.com/p/speed/wiki/Phacter

我没有试过这个,不过我觉得这个想法是不错的。有些人可能不想或者不能在他们的系统上安装Ruby,但他们还是希望有类似的功能。

6

我看不出有什么理由不直接使用facter这个工具。它的输出格式很容易在Python脚本中使用。

import subprocess
import pprint

def facter2dict( lines ):
        res = {}
        for line in lines:
                k, v = line.split(' => ')
                res[k] = v
        return res

def facter():
        p = subprocess.Popen( ['facter'], stdout=subprocess.PIPE )
        p.wait()
        lines = p.stdout.readlines()
        return facter2dict( lines )

if __name__ == "__main__":
        pprint.pprint( facter() )

撰写回答