python常用运维脚本实例小结常用运维脚本实例小结
主要介绍了python常用运维脚本实例小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学
习吧
一、一、ps 可以查看进程的内存占用大小,写一个脚本计算一下所有进程所占用内存大小的和。可以查看进程的内存占用大小,写一个脚本计算一下所有进程所占用内存大小的和。
(提示,使用ps aux 列出所有进程,过滤出RSS那列,然后求和)
注:ps -ef 与 ps aux 效果一样使用随意
import os
list = []
sum = 0
str1 = os.popen('ps aux','r').readlines()
for i in str1:
str2 = i.split()
new_rss = str2[5]
list.append(new_rss)
for i in list[1:-1]:
num = int(i)
sum = sum + num
print '%s:%s' %(list[0],sum)
二、一键部署二、一键部署lvs与与keepalived
注:部署keepalived配置文件,会自动将lvs部署好,不必单独搭建,但是要下载好ipvsadm
这里单独搭建了lvs
#提前配置好免密钥登陆,与apache服务
import pexpect
import os
ds_ip = '192.168.102.143'
rs1_ip = '192.168.102.144'
rs2_ip = '192.168.102.145'
vip = '192.168.102.250'
ds_cmd = ['ipvsadm -C',
'ipvsadm -A -t {vip}:80 -s rr'.format(vip=vip),
'ipvsadm -a -t {vip}:80 -r {rs1_ip}:80 -g'.format(vip=vip, rs1_ip=rs1_ip),
'ipvsadm -a -t {vip}:80 -r {rs2_ip}:80 -g'.format(vip=vip,rs2_ip=rs2_ip),
'ifconfig ens33:0 {vip} broadcast {vip} netmask 255.255.255.255 up'.format(vip=vip),
'route add -host {vip} dev ens33:0'.format(vip=vip)
]
# for ds_cmd in ds_cmd:
rs1_cmd = [
'ansible rs1 -m shell -a "ifconfig lo:0 {vip} broadcast {vip} netmask 255.255.255.255 up" &>/dev/null'.format(vip=vip),
'ansible rs1 -m shell -a "route add -host {vip} dev lo:0" &>/dev/null'.format(vip=vip),
'ansible rs1 -m shell -a "echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore" &>/dev/null',
'ansible rs1 -m shell -a "echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce" &>/dev/null',
'ansible rs1 -m shell -a "echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore" &>/dev/null',
'ansible rs1 -m shell -a "echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce" &>/dev/null',
'service httpd restart']
# for rs_cmd in rs_cmd:
rs2_cmd = [
'ansible rs2 -m shell -a "ifconfig lo:0 {vip} broadcast {vip} netmask 255.255.255.255 up" &>/dev/null'.format(vip=vip),
'ansible rs2 -m shell -a "route add -host {vip} dev lo:0" &>/dev/null'.format(vip=vip),
'ansible rs2 -m shell -a "echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore" &>/dev/null',
'ansible rs2 -m shell -a "echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce" &>/dev/null',
'ansible rs2 -m shell -a "echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore" &>/dev/null',
'ansible rs2 -m shell -a "echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce" &>/dev/null',
'service httpd restart']
ansible='''
echo "[rs1]" >> /etc/ansible/hosts;
echo "{rs1_ip}" >> /etc/ansible/hosts;
echo "[rs2]" >> /etc/ansible/hosts;
echo "{rs2_ip}" >> /etc/ansible/hosts;
'''.format(rs1_ip=rs1_ip,rs2_ip=rs2_ip)
def Ansible():
res = os.system('yum install ansible -y &>/dev/null')
if res== 0:
print('ansible安装完成!')
os.system(ansible)
def lvs():
i = os.system('yum -y install ipvsadm &>/dev/null')
if i == 0:
print('lvs下载完成')
for a in ds_cmd:
res = os.system(a)
if res == 0:
print('ok')
else:
print('false')
for b in rs1_cmd:
res1 = os.system(b)
if res1 == 0:
print('ok')
else:
print('false')
for c in rs2_cmd:
res2 = os.system(c)
if res2 == 0:
print('ok')
else:
print('false')
else:
print('lvs下载失败')
keepalived = '''
#全局配置
! Configuration File for keepalived