aix安装nmon

aix5310以上都系统自带了nmon,其他低版本需要手动安装

软件包下载地址https://www.ibm.com/developerworks/community/wikis/home?lang=en#/wiki/Power%20Systems/page/nmon

安装脚本如下

import os
from subprocess import Popen, PIPE, STDOUT
import sys
import platform
import shutil


def run_cmd(cmd, cwd=None, env=None, run_as=None):
if not sys.platform.startswith('win') and run_as and run_as != 'root':
cmd = 'su - {} -c "{}"'.format(run_as, cmd)
p = Popen(cmd, shell=True, stdout=PIPE, stderr=STDOUT, stdin=PIPE, cwd=cwd, env=env)
stdout, _ = p.communicate()
return p.returncode, stdout.strip()


if 'aix' not in platform.platform().lower():
print '只支持aix系统的安装,该机器不是aix系统'
sys.exit(1)

cmd = 'oslevel -r'
version_list={'5':5309,'6':6102,'7':7000}
code, res = run_cmd(cmd)
if not code and res:
key=res[0].strip()
value=res.split('-')[1].strip()
if version_list.get(key) and int(key+res[1].strip()+value)>=version_list.get(key):
print '当前版本是',res
else:
print '当前版本是',res,'该操作只支持5309+,6102+,7100+的版本'
sys.exit(1)
else:
print '获取版本失败'
sys.exit(1)

cmd = 'ps -ef |grep nmon|grep -v grep'
code, res = run_cmd(cmd)
if res:
print '已有nmon在运行'
sys.exit(1)

cmd = 'which nmon'
code, res = run_cmd(cmd)
if code:
print '该系统未安装nmon'
sys.exit(1)

if not os.path.exists(data_path):
os.makedirs(data_path)


def is_empty(path):
for root, dirs, files in os.walk(path):
if len(files) != 0:
return False
return True


cronfile = '/var/spool/cron/crontabs/root'
run_cmd('cp {0} /tmp/crontab_root.`date +%F_%T`'.format(cronfile))


content = '0 0 * * * nmon -fT -m {0} -s {1} -c {2} '
'0 0 * * * find {1} -mtime +{3} -name "*.nmon" -exec rm -rf {{}} ; '.format(data_path, save_day, count,gap_time)


has_content=False
if not os.path.exists(cronfile):
with open(cronfile, 'w') as f:
f.writelines(['### for nmon install ### ',content,'### for nmon install ### '])
else:
real_content=[]
with open(cronfile, 'r') as f:
raw_content = f.readlines()

flag=0
for i in raw_content:
flag = flag % 2
if '### for nmon install ###' in i.strip():
flag += 1
if flag==0:
real_content.append(i)

with open(cronfile, 'w') as f:
f.writelines(real_content)
with open(cronfile, 'a') as f:
f.writelines(['### for nmon install ### ',content,'### for nmon install ### '])

print '安装并配置成功'




  

原文地址:https://www.cnblogs.com/slqt/p/10494493.html