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
#全局配置
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()

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

(0)

相关推荐

  • python钉钉机器人运维脚本监控实例

    如下所示: #!/usr/bin/python3 # -*- coding:UTF-8-*- # Author: zhuhongqiang from urllib import request import json from sys import argv access_token = "xxx" def send_msg(mobile, item_name): """ 钉钉机器人API接口地址: https://open-doc.dingtalk.co

  • 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

  • 利用Python编写的实用运维脚本分享

    目录 1. 执行外部程序或命令 2. 文件和目录操作(命名.删除.拷贝.移动等) 3. 创建和解包归档文件 参考 Python在很大程度上可以对shell脚本进行替代.笔者一般单行命令用shell,复杂点的多行操作就直接用Python了.这篇文章就归纳一下Python的一些实用脚本操作. 1. 执行外部程序或命令 我们有以下C语言程序cal.c(已编译为.out文件),该程序负责输入两个命令行参数并打印它们的和.该程序需要用Python去调用C语言程序并检查程序是否正常返回(正常返回会返回 0)

  • Python自动化运维之Ansible定义主机与组规则操作详解

    本文实例讲述了Python自动化运维之Ansible定义主机与组规则操作.分享给大家供大家参考,具体如下: 一 点睛 Ansible通过定义好的主机与组规则(Inventory)对匹配的目标主机进行远程操作,配置规则文件默认是/etc/ansible/hosts. 二 定义主机与组 所有定义的主机与组规则都在/etc/Ansible/hosts文件中,为ini文件格式,主机可以用域名.IP.别名进行标识,其中webservers.dbservers 为组名,紧跟着的主机为其成员.格式如下: ma

  • 10大HBase常见运维工具整理小结

    摘要:HBase自带许多运维工具,为用户提供管理.分析.修复和调试功能.本文将列举一些常用HBase工具,开发人员和运维人员可以参考本文内容,利用这些工具对HBase进行日常管理和运维. HBase组件介绍 HBase作为当前比较热门和广泛使用的NoSQL数据库,由于本身设计架构和流程上比较复杂,对大数据经验较少的运维人员门槛较高,本文对当前HBase上已有的工具做一些介绍以及总结. 写在前面的说明: 1) 由于HBase不同版本间的差异性较大(如HBase2.x上移走了hbck工具),本文使用

  • python自动化运维之Telnetlib的具体使用

    目录 前言: 1.导入telnetlib库可以直接使用. 2.配置服务器.用户名.密码,cmd命令等 3.功能函数 前言: 远程连接中兴设备(系统使用的中兴网卡)时使用的事Telnet连接,连接时设有二次验证,每次输入用户名密码和执行命令是个繁琐的过程,使用Python自带的telnetlib库可以编写脚本,实现批量登录服务器并执行命令查询数据量. 1.导入telnetlib库可以直接使用. from telnetlib import Telnet 2.配置服务器.用户名.密码,cmd命令等 #

  • Python创建二维数组实例(关于list的一个小坑)

    0.目录 1.遇到的问题 2.创建二维数组的办法 •3.1 直接创建法 •3.2 列表生成式法 •3.3 使用模块numpy创建 1.遇到的问题 今天写Python代码的时候遇到了一个大坑,差点就耽误我交作业了... 问题是这样的,我需要创建一个二维数组,如下: m = n = 3 test = [[0] * m] * n print("test =", test) 输出结果如下: test = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] 是不是看起来没有一点问

  • Python自动化运维_文件内容差异对比分析

    模块:difflib 安装:Python版本大于等于2.3系统自带 功能:对比文本之间的差异,而且支持输出可读性比较强的HTML文档,与Linux中的diff命令比较相似. 两个字符串的差异对比: #import difflib #text1=''' #hello world. #how are you. #nice to meet you. #''' #text1_lines=text1.splitlines() # 以行进行分割,便于进行对比 #text2=''' #Hello World.

  • python 一维二维插值实例

    一维插值 插值不同于拟合.插值函数经过样本点,拟合函数一般基于最小二乘法尽量靠近所有样本点穿过.常见插值方法有拉格朗日插值法.分段插值法.样条插值法. 拉格朗日插值多项式:当节点数n较大时,拉格朗日插值多项式的次数较高,可能出现不一致的收敛情况,而且计算复杂.随着样点增加,高次插值会带来误差的震动现象称为龙格现象. 分段插值:虽然收敛,但光滑性较差. 样条插值:样条插值是使用一种名为样条的特殊分段多项式进行插值的形式.由于样条插值可以使用低阶多项式样条实现较小的插值误差,这样就避免了使用高阶多项

  • 利用python为运维人员写一个监控脚本

    前言: 一直想写一个监控方面的脚本,然后想到了运维这方面的,后来就写了个脚本,下面话不多说了,来一起看看详细的介绍吧. 准备: psutil模块(基本使用方法可以参考这篇文章:http://www.jb51.net/article/65044.htm) 正文: import os import time import re import smtplib from email.mime.text import MIMEText from email.header import Header imp

随机推荐