SaltStack运维实战 ----代码整理

3.5 编写一个完整模快

# -*- coding: utf-8 -*-
 '''
The top nth processes which take up CPU and memory space usage are available
through this module, additionaly;
the module can get the system load information.
 '''
# Import python libs
import os
# Import salt libs
import salt.utils

def cpu(n):
    '''
    Return the top nth processes which take up the memory usage for this
minion
    CLI Example:
        salt '*' prank.cpu <n>
     '''
    cmd = "ps ax|sort -k3 -nr|head -n%s" %str(n)
    output = __salt__['cmd.run_stdout'](cmd,python_shell=True) 
    res=[]
    for line in output.splitlines():
	res.append(line)
    return output

def mem(n):
    '''
    Return the top nth processes which take up the CPU usage for this minion
    CLI Example:
        salt '*' prank.cpu <n>
     '''
    cmd = "ps axu|sort -k4 -nr|head -n%s" % str(n)
    output = __salt__['cmd.run_stdout'](cmd,python_shell=True)
    res = []
    for line in output.splitlines():
            res.append(line)
    return res



def load():
    '''
   Return the load averages for this minion
   CLI Example:
    .. code-block:: bash
       salt '*' prank.loadavg
     '''
    load_avg = os.getloadavg()
    return {'1-min': load_avg[0],
            '5-min': load_avg[1],
            '15-min': load_avg[2]}

问题:
发现原文中没有这个参数 python_shell=True
执行报错,加上后问题解决。

然后查找文档发现这么一句
:param bool python_shell:
If False, let python handle the positional arguments.
Set to True to use shell features, such as pipes or redirection.

文档链接
https://www.cnblogs.com/randomlee/p/Saltstack_module_cmd.html

发现cmd命令中如果有| 管道符 等符号 ,没 有python_shell=True参数,运行会报错。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值