python常用运维脚本实例小结


Posted in Python onFebruary 14, 2020

一、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
#全局配置
global_defs {
  notification_email {
    #收件人地址
  }
    #邮件服务器
}
VRRP配置DS1
vrrp_instance VI_1 {
  state MASTER          #角色类型MASTER|BACKUP
  interface ens33          #网卡名称
  virtual_router_id 51      #虚拟路由id(需要与BACKUP一致)
  priority 100          #优先级
  advert_int 1          #没1秒检查一次
  #nopreempt            #非抢占模式

  authentication {
    auth_type PASS        #认证类型 主备之间必须一样
    auth_pass 1111        #认证密码 主备之间必须一样
  }
  virtual_ipaddress {
    192.168.102.250        #虚拟ip(vip)
  }
}

#LVS配置
virtual_server 192.168.102.250 80 {
  delay_loop 3          #健康检查时间间隔
  lb_algo rr            #负载均衡调度算法
  lb_kind DR            #负载均衡转发规则
  protocol TCP          #协议

  real_server 192.168.102.144 80 {  #要监控的real_server的ip和端口号
      weight 1         #权重
    TCP_CHECK {          #基于tcp协议的检查
      connect_timeout 3     #连接时间超时
      retry 3          #重连次数
      delay_before_retry 3  #重连间隔时间
    }
  }


  real_server 192.168.102.145 80 {
      weight 1
    TCP_CHECK {
      connect_timeout 3
      retry 3
      delay_before_retry 3
    }
  }
}'''
def Keepalived():
  d = os.system('yum install keepalived -y &> /dev/null')
  if d == 0:
    print('keeplived下载完成')
    with open(r'/etc/keepalived/keepalived.conf', 'w', encoding='utf-8') as f:
      f.write(keepalived)
    e = os.system('systemctl start keepalived')
    if e == 0:
      print('keepalived部署完成!')
    else:
      print('keepalived部署失败')
  else:
    print('keepalived下载失败')

def main():
  # key()
  Ansible()
  # ssh_copy()
  lvs()
  Keepalived()

if __name__ == '__main__':
  main()

三、用python3编写一个监控上面lvs服务状态和系统资源的脚本

import yagmail
import paramiko
import socket
import re
import time
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
rs1_ip = '192.168.102.144'
rs2_ip = '192.168.102.145'
ds_ip = '192。168.102.143'
disk_cmd = "df -m | grep /dev/sda1 | awk '{print $4}'"
cpu_cmd = '''
top n 1 d 1 b -p 1 | grep '%Cpu' | awk -F "," '{print $4}' | awk -F "id" '{print $1}' | awk -F "." '{print $1}'
'''
free_cmd = "free -m | grep Mem |awk '{print $7}'"

def connect1():
  ssh.connect('{rs1_ip}'.format(rs1_ip=rs1_ip), 22, 'root', '1', timeout=3)
def connect2():
  ssh.connect('{rs2_ip}'.format(rs2_ip=rs1_ip), 22, 'root', '1', timeout=3)

def mail(head, Contents):
  yag = yagmail.SMTP(user="15898297124@163.com", password="zz54605519", host='smtp.163.com')
  yag.send('15898297124@163.com',subject = head, contents = Contents)
  yag.close()

def http():
  socket.setdefaulttimeout(1)
  server = socket.socket()
  host_list = ['%s:80' % (rs1_ip),'%s:80' % (rs2_ip)]
  for info in host_list:
    ip = re.compile('(.*?):(.*)').search(info).group(1)
    port = re.compile('(.*?):(.*)').search(info).group(2)
    res = server.connect_ex((ip, int(port)))
    if res != 0:
      mail(ip,'%s不通' % port)

def disk():
  connect1()
  stdin1, stdout1, stderr1 = ssh.exec_command(disk_cmd)
  disk1 = int(stdout1.read().decode('utf-8'))
  print('%s节点disk剩余空间为%sM' % (rs1_ip,disk1))
  if disk1 <= 200:
    mail(rs1_ip,'disk剩余空间为%sM' % disk1)
  connect2()
  stdin2, stdout2, stderr2 = ssh.exec_command(disk_cmd)
  disk2 = int(stdout2.read().decode('utf-8'))
  print('%s节点disk剩余空间为%sM' % (rs2_ip, disk2))
  if disk2 <= 200:
    mail(rs2_ip, 'disk剩余空间为%sM' % disk2)

def cpu():
  connect1()
  stdin1, stdout1, stderr1 = ssh.exec_command(cpu_cmd)
  cpu1 = int(stdout1.read().decode('utf-8'))
  cpu = 100 - cpu1
  print('%s节点cpu使用率为百分之%s' % (rs1_ip, cpu))
  if cpu >= 80:
    mail(rs1_ip, 'cpu使用量为%s' % cpu)

  connect2()
  stdin2, stdout2, stderr2 = ssh.exec_command(cpu_cmd)
  cpu2 = int(stdout2.read().decode('utf-8'))
  cpu = 100 - cpu2
  print('%s节点cpu使用率为百分之%s' % (rs2_ip, cpu))
  if cpu >= 80:
    mail(rs2_ip, 'cpu使用量为%s' % cpu)

def free():
  connect1()
  stdin1, stdout1, stderr1 = ssh.exec_command(free_cmd)
  free1 = int(stdout1.read().decode('utf-8'))
  print('%s节点磁盘剩余空间为%sM' % (rs1_ip, free1))
  if free1 <= 200:
    mail(ds_ip, '磁盘剩余空间为%sM' % free1)

  connect2()
  stdin2, stdout2, stderr2 = ssh.exec_command(disk_cmd)
  free2 = int(stdout2.read().decode('utf-8'))
  print('%s节点磁盘剩余空间为%sM' % (rs2_ip, free2))
  if free2 <= 200:
    mail(ds_ip, '磁盘剩余空间为%sM' % free2)

def main():
  while True:
    http()
    cpu()
    free()
    disk()
    time.sleep(60)

if __name__ == '__main__':
  main()

四、用python编写一键部署数据库主从服务

主服务器

import pexpect
import os
import configparser

# HOSTNAME_DB1='db1'
# HOSTNAME_DB2='db2'
# DB1 = '192.168.254.24'
# DB2 = '192.168.254.27'
DBPASSWORD = '1'

def repo():
  os.system('touch /etc/yum.repos.d/mariadb.repo')
  with open('/etc/yum.repos.d/mariadb.repo','w',encoding='utf8') as f:
    f.write('[mariadb]')
  config = configparser.ConfigParser()
  config.read("/etc/yum.repos.d/mariadb.repo", encoding="utf-8")
  config.set('mariadb', 'name', 'MariaDB')
  config.set('mariadb', 'baseurl', 'http://mirrors.ustc.edu.cn/mariadb/yum/10.3/centos7-amd64/')
  config.set('mariadb', 'gpgkey', 'http://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB')
  config.set('mariadb', 'gpgcheck', '1')
  config.write(open('/etc/yum.repos.d/mariadb.repo','w'))
def mariadb():
  a = os.system('yum install MariaDB -y &> /dev/null')
  if a == 0:
    b = os.system('systemctl start mariadb &> /dev/null')
    if b == 0:
      print('mariadb启动成功')
    child = pexpect.spawn('mysql_secure_installation')
    child.expect('enter for none')
    child.sendline('')
    child.expect('Y/n')
    child.sendline('y')
    child.expect('New')
    child.sendline(DBPASSWORD)
    child.expect('Re-enter')
    child.sendline(DBPASSWORD)
    child.expect('successfully')
    child.sendline('')
    child.sendline('')
    child.sendline('')
    child.sendline('')
    child.interact()
    child.close()
def db1():
  config = configparser.ConfigParser()
  config.read("/etc/my.cnf.d/server.cnf", encoding="utf-8")
  config.set('mysqld', 'server-id', '1')
  config.set('mysqld', 'log-bin', 'mysql-bin')
  config.write(open('/etc/my.cnf.d/server.cnf','w'))
  b = os.system('systemctl restart mariadb')
  if b == 0:
    # os.system('mysql -uroot -p%s' % DBPASSWORD)
    # os.system("CREATE USER 'slave'@'%' IDENTIFIED BY 'slave';")
    # os.system("GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%';")
    # os.system('flush privileges;')
    # os.system('show master status')
    child = pexpect.spawn('mysql -uroot -p1')
    child.expect('none')
    child.sendline("CREATE USER 'slave'@'%' IDENTIFIED BY 'slave';")
    child.expect('none')
    child.sendline("GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%';")
    child.expect('none')
    child.sendline('flush privileges;')
    child.expect('none')
    child.sendline('show master status;')
    child.interact()
    child.close()
def main():
  repo()
  mariadb()
  db1()
if __name__ == '__main__':
  main()

从服务器

import pexpect
import os
import configparser

# HOSTNAME_DB1='db1'
# HOSTNAME_DB2='db2'
DB1 = '192.168.102.143'
# DB2 = '192.168.254.27'
DBPASSWORD = '1'

def repo():
  os.system('touch /etc/yum.repos.d/mariadb.repo')
  with open('/etc/yum.repos.d/mariadb.repo','w',encoding='utf8') as f:
    f.write('[mariadb]')
  config = configparser.ConfigParser()
  config.read("/etc/yum.repos.d/mariadb.repo", encoding="utf-8")
  config.set('mariadb', 'name', 'MariaDB')
  config.set('mariadb', 'baseurl', 'http://mirrors.ustc.edu.cn/mariadb/yum/10.3/centos7-amd64/')
  config.set('mariadb', 'gpgkey', 'http://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB')
  config.set('mariadb', 'gpgcheck', '1')
  config.write(open('/etc/yum.repos.d/mariadb.repo','w'))

def mariadb():
  a = os.system('yum install MariaDB -y')
  if a == 0:
    b = os.system('systemctl start mariadb &> /dev/null')
    if b == 0:
      print('mariadb启动成功')
    child = pexpect.spawn('mysql_secure_installation')
    child.expect('enter for none')
    child.sendline('')
    child.expect('Y/n')
    child.sendline('y')
    child.expect('New')
    child.sendline(DBPASSWORD)
    child.expect('Re-enter')
    child.sendline(DBPASSWORD)
    child.expect('successfully')
    child.sendline('')
    child.sendline('')
    child.sendline('')
    child.sendline('')

def db2():
  config = configparser.ConfigParser()
  config.read("/etc/my.cnf.d/server.cnf", encoding="utf-8")
  config.set('mysqld', 'server-id', '2')
  config.write(open('/etc/my.cnf.d/server.cnf','w'))
  b = os.system('systemctl restart mariadb')
  if b == 0:
    bin = 'mysql-bin.000001'
    pos = 765
    # os.system('mysql -uroot -p%s' % DBPASSWORD)
    # os.system('''mysql -uroot -p%s -e "CHANGE MASTER TO MASTER_HOST='%s', MASTER_USER='slave', MASTER_PASSWORD='slave', MASTER_LOG_FILE='%s', MASTER_LOG_POS=%s;"''' % (DBPASSWORD,DB1,bin,pos))
    # os.system("mysql -uroot -p%s -e 'start slave;'" % DBPASSWORD)
    child = pexpect.spawn('mysql -uroot -p%s' % DBPASSWORD)
    child.expect('none')
    child.sendline("CHANGE MASTER TO MASTER_HOST='%s', MASTER_USER='slave', MASTER_PASSWORD='slave', MASTER_LOG_FILE='%s', MASTER_LOG_POS=%s;" % (DB1,bin,pos))
    child.expect('none')
    child.sendline( 'start slave;' )
    child.interact()
    child.close()
def main():
  repo()
  mariadb()
  db2()
if __name__ == '__main__':
  main()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
提升Python程序运行效率的6个方法
Mar 31 Python
Python实现从URL地址提取文件名的方法
May 15 Python
python实现提取百度搜索结果的方法
May 19 Python
Python安装第三方库的3种方法
Jun 21 Python
python虚拟环境virtualenv的安装与使用
Sep 21 Python
Python编程产生非均匀随机数的几种方法代码分享
Dec 13 Python
关于 Python opencv 使用中的 ValueError: too many values to unpack
Jun 28 Python
对Python中小整数对象池和大整数对象池的使用详解
Jul 09 Python
PyTorch中反卷积的用法详解
Dec 30 Python
tensorboard显示空白的解决
Feb 15 Python
Python实现在Windows平台修改文件属性
Mar 05 Python
Pytorch 中的optimizer使用说明
Mar 03 Python
Python如何在DataFrame增加数值
Feb 14 #Python
python turtle工具绘制四叶草的实例分享
Feb 14 #Python
Python阶乘求和的代码详解
Feb 14 #Python
Python pip配置国内源的方法
Feb 14 #Python
Python3基本输入与输出操作实例分析
Feb 14 #Python
From CSV to SQLite3 by python 导入csv到sqlite实例
Feb 14 #Python
pandas实现excel中的数据透视表和Vlookup函数功能代码
Feb 14 #Python
You might like
PHP生成静态页
2006/11/25 PHP
php 向访客和爬虫显示不同的内容
2009/11/09 PHP
php include加载文件两种方式效率比较
2010/08/08 PHP
PHP实现批量清空删除指定文件夹所有内容的方法
2017/05/30 PHP
Thinkphp5.0框架的Db操作实例分析【连接、增删改查、链式操作等】
2019/10/11 PHP
JS实现浏览器菜单命令
2006/09/05 Javascript
jQuery时间轴插件使用详解
2015/07/16 Javascript
JavaScript实现带缓冲效果的随屏滚动漂浮广告代码
2015/11/06 Javascript
KnockoutJS 3.X API 第四章之数据控制流foreach绑定
2016/10/10 Javascript
ExtJS 4.2 Grid组件单元格合并的方法
2016/10/12 Javascript
详解百度百科目录导航树小插件
2017/01/08 Javascript
基于js的变量提升和函数提升(详解)
2017/09/17 Javascript
详解用Node.js实现Restful风格webservice
2017/09/29 Javascript
JavaScript html5 canvas实现图片上画超链接
2017/10/20 Javascript
React Native时间转换格式工具类分享
2017/10/24 Javascript
微信小程序获取音频时长与实时获取播放进度问题
2018/08/28 Javascript
微信小程序实现的3d轮播图效果示例【基于swiper组件】
2018/12/11 Javascript
JavaScript中AOP的实现与应用
2019/05/06 Javascript
vue 扩展现有组件的操作
2020/08/14 Javascript
Vue中component标签解决项目组件化操作
2020/09/04 Javascript
webpack4从0搭建组件库的实现
2020/11/29 Javascript
Vue实现指令式动态追加小球动画组件的步骤
2020/12/18 Vue.js
python使用clear方法清除字典内全部数据实例
2015/07/11 Python
python先序遍历二叉树问题
2017/11/10 Python
python Flask实现restful api service
2017/12/04 Python
Flask框架实现给视图函数增加装饰器操作示例
2018/07/16 Python
python实现微信自动回复机器人功能
2019/07/11 Python
Python使用turtle库绘制小猪佩奇(实例代码)
2020/01/16 Python
Python异常原理及异常捕捉实现过程解析
2020/03/25 Python
CSS3教程(5):网页背景图片
2009/04/02 HTML / CSS
Electric官网:美国高级眼镜和配件品牌
2020/06/04 全球购物
党的群众路线教育实践活动批评与自我批评
2014/02/16 职场文书
祖国在我心中演讲稿400字
2014/05/04 职场文书
考试没考好检讨书
2015/05/06 职场文书
Pyhton爬虫知识之正则表达式详解
2022/04/01 Python
python中urllib包的网络请求教程
2022/04/19 Python