自动化运维平台搭建

网友投稿 858 2022-10-14

本站部分文章、图片属于网络上可搜索到的公开信息,均用于学习和交流用途,不能代表睿象云的观点、立场或意见。我们接受网民的监督,如发现任何违法内容或侵犯了您的权益,请第一时间联系小编邮箱jiasou666@gmail.com 处理。

自动化运维平台搭建

项目简介:

项目介绍:自动化运维是未来的趋势,最近学了不少东西,正好通过这个小项目把这些学的东西串起来,练练手。

基础架构:

服务器端:web框架-Django前端:html css jQuery bootstrap脚本:shell适用系统:redhat5.8/redhat6.6

平台已实现功能:

中间件和数据库软件的启停和状态检查 (tomcat,nginx,apache,oracle,mysql)

完整功能设计图:

效果图:

架构图(简要):

shell脚本

1 #!/bin/sh 2 #Filename:starttomcat.sh 3 #需要传入参数:$1 $2 $3 4 # $1:tomcat的home目录 5 # $2:端口号 6 # $3:启动tomcat超时时长 7 #输出结果说明: 8 # 101:启动成功 9 # 104:启动超时10 11 #写日志函数12 log(){13 echo `date +"%F %T"`" "$* >> /logs/tomcat.log14 }15 16 #开启tomcat函数17 starttomcat(){18 log "[command]:"$0" [parameters]:"$*19 #启动tomcat前需要调用checktomcat获取tomcat状态20 status=`. /operation/tomcat/checktomcat.sh $1 $2`21 #如果tomcat处于运行状态,就不必再启动,把状态信息返回前端22 if [ $status -eq 101 ];then23 echo 10124 #如果tomcat未运行,执行启动操作25 else26 su - tomcat -c $1/bin/startup.sh >/dev/null 2>&127 #设置count计时28 count=029 #每5秒检查一次tomcat状态,直到检查到tomcat成功启动或者超时30 status=`. /operation/tomcat/checktomcat.sh $1 $2`31 until [ $status -eq 101 ] || [ $count -ge $3 ]32 do33 sleep 534 let count=$count+535 status=`. /operation/tomcat/checktomcat.sh $1 $2`36 done37 #如果检测到tomcat正常,判断为启动成功,返回状态信息38 if [ $status -eq 101 ];then39 echo 10140 #如果超时还未启动成功,则判断为启动超时,返回启动超时代号10441 else42 echo 10443 fi44 fi45 }46 47 starttomcat $1 $2 $3

views.py:tomcat操作响应函数

1 # 针对tomcat服务器的操作: 2 # 1.首先通过前台获得ID 和 操作 3 # 2.通过ID 丰富信息 4 # 3.形成完整的操作SQL 5 # 4.执行SQL,返回结果 6 # 5.将操作信息及结果写入操作记录表,并将结果返回前台 7 # 6.前台收到信息更新tomcat现在运行状态 8 def operation(request): 9 # 获得前台信息10 tomcat_id = request.GET.get('id')11 tomcat_action = request.GET.get('action')12 oper = request.COOKIES.get('loginname')13 # 根据ID和action 获得任务信息,并形成完整的操作SQL,都存入taskinfo中14 taskinfo = get_taskinfo(tomcat_id, tomcat_action, oper)15 # 传入taskinfo,执行SQL操作,返回目标服务器控制台的结果16 mytask = Task(taskinfo)17 result = mytask.execute()18 if result.isdigit():19 taskinfo['resulut'] = result20 else:21 taskinfo['resulut'] = '102'22 # 将操作记录写入记录表中,同时更新tomcatdata表中的状态字段23 genrecords_updatestatus(taskinfo)24 # 将结果传到前台25 message = {26 '101': 'Tomcat正常运行.',27 '102': 'Tomcat异常,请人工检查.',28 '103': 'Tomcat服务关闭.',29 '104': 'Tomcat启动超时.',30 '105': 'Tomcat关闭超时.',31 }32 return JsonResponse({33 'status': taskinfo['resulut'],34 'message': message[taskinfo['resulut']],35 })36 37 # 根据ID生成taskinfo38 def get_taskinfo(tomcat_id, tomcat_action, oper):39 with connection.cursor() as cursor:40 cursor.execute(41 'SELECT id, tomcatport, tomcathome, ipaddress, startwait, stopwait FROM tomcatdata WHERE id = %s' % tomcat_id42 )43 tomcater = dictfetchall(cursor)[0]44 serverip = tomcater['ipaddress']45 cursor.execute(46 "SELECT user1,password1 FROM machine_pwd WHERE ipaddress = '%s'" % serverip47 )48 userinfo = dictfetchall(cursor)[0]49 if tomcat_action == 'check_tomcat':50 tomcat_home = tomcater['tomcathome']51 tomcat_port = tomcater['tomcatport']52 command = 'sh /operation/tomcat/checktomcat.sh %s %s ' % (tomcat_home, tomcat_port)53 elif tomcat_action == 'start_tomcat':54 # 需要传入三个参数 home目录/端口号/启动超时时长55 tomcat_home = tomcater['tomcathome']56 tomcat_port = tomcater['tomcatport']57 start_wait = tomcater['startwait']58 # sh_dir = '/operation/tomcat/starttomcat.sh'59 command = 'sh /operation/tomcat/starttomcat.sh %s %s %s ' % (tomcat_home, tomcat_port, start_wait)60 elif tomcat_action == 'stop_tomcat':61 # 需要传入三个参数 home目录/端口号/启动超时时长62 tomcat_home = tomcater['tomcathome']63 tomcat_port = tomcater['tomcatport']64 stop_wait = tomcater['stopwait']65 # sh_dir = '/operation/tomcat/starttomcat.sh'66 command = 'sh /operation/tomcat/stoptomcat.sh %s %s %s ' % (tomcat_home, tomcat_port, stop_wait)67 task_info = {68 'id': tomcat_id,69 'action': tomcat_action,70 'oper': oper,71 'ip': tomcater['ipaddress'],72 'user': userinfo['user1'],73 'pwd': userinfo['password1'],74 'cmd': command,75 'result': ''76 }77 return task_info78 79 80 # 写入操作记录并更新tomcat状态81 def genrecords_updatestatus(taskinfo):82 with connection.cursor() as cursor:83 sqlstatement1 = "insert into audit_log (oper_user, oper_command, oper_message) VALUES ('%s', '%s', '%s')" % (84 taskinfo['oper'], taskinfo['cmd'], taskinfo['resulut'])85 sqlstatement2 = "update tomcatdata set status = %d where id = %r" % (int(taskinfo['resulut']), taskinfo['id'])86 cursor.execute(sqlstatement1)87 cursor.execute(sqlstatement2)

Task类

1 import paramiko 2 3 4 class Task(object): 5 def __init__(self, task_info): 6 self.task_info = task_info 7 8 def create_connect(self): 9 # 创建SSH对象10 connectObj = paramiko.SSHClient()11 # 把要连接的机器添加到known_hosts文件中12 connectObj.set_missing_host_key_policy(paramiko.AutoAddPolicy())13 # 连接服务器14 connectObj.connect(15 hostname=self.task_info['ip'],16 username=self.task_info['user'],17 password=self.task_info['pwd'],18 port=22,19 timeout=1020 )21 # 判断是否连接成功22 ###################23 return connectObj24 25 def execute(self):26 try:27 connectObj = self.create_connect()28 except:29 return '102'30 cmd = self.task_info['cmd']31 stdin, stdout, stderr = connectObj.exec_command(cmd)32 result = stdout.read()33 connectObj.close()34 if not result:35 result = stderr.read().strip()36 return result.decode().strip()

js文件

1 $(function () { 2 $("ul[id='servicemgr'] li").click(function () { 3 4 if (this.id == 'toms') { 5 $("#workpage").empty().load("/static/maintenance/html/workpage.html #tom_workpage"); 6 $.ajax({ 7 type: "GET", 8 url: "./../tomcatData/", 9 datatype: 'json', 10 data: {page: 1}, 11 success: function (datas) { 12 loadtomcatdata(datas) 13 } 14 }); 15 } else if (this.id == 'oras') { 16 $("#workpage").empty().load("/static/maintenance/html/workpage.html #ora_workpage"); 17 $.ajax({ 18 type: "GET", 19 url: "./../oracleData/", 20 datatype: 'json', 21 success: function (datas) { 22 loadoracledata(datas) 23 } 24 }) 25 } 26 }); 27 }); 28 29 // 针对tomcat服务器的操作 30 function opt_tomcat(obj) { 31 var tomcat_mes = $("#tomcat_mes"); 32 tomcat_mes.empty().append("正在玩命操作,请等待…"); 33 var id = obj.id; 34 var action = obj.name; 35 $.ajax({ 36 type: 'Get', 37 url: './../operation', 38 data: {'id': id, 'action': action}, 39 success: function (data) { 40 tomcat_mes.empty().append(data['message']); 41 //更新状态 42 if (data['status'] == '101') { 43 $(obj).parent().prevAll('.status').children('span').attr('class', 'glyphicon glyphicon-ok-sign') 44 } else if (data['status'] == '102' || data['status'] == '104' || data['status'] == '105') { 45 $(obj).parent().prevAll('.status').children('span').attr('class', 'glyphicon glyphicon-exclamation-sign') 46 } else if (data['status'] == '103') { 47 $(obj).parent().prevAll('.status').children('span').attr('class', 'glyphicon glyphicon-remove-sign') 48 } 49 } 50 }) 51 } 52 // 分页 53 function page(obj) { 54 var page_number = $(obj).text(); 55 $.ajax({ 56 type: "GET", 57 url: "./../tomcatData/", 58 datatype: 'json', 59 data: {page: page_number}, 60 success: function (datas) { 61 loadtomcatdata(datas) 62 } 63 }); 64 } 65 //导入tomcat数据 66 function loadtomcatdata(datas) { 67 var text = $('.text'); 68 text.empty(); 69 var html = ''; 70 for (var i = 0; i < datas.length; i++) { 71 var id = datas[i]['id']; 72 var ip = datas[i]['ipaddress']; 73 var host = datas[i]['machine']; 74 var dec = datas[i]['description']; 75 var status = datas[i]['status']; 76 html += ''; 77 html += '' + id + ''; 78 html += '' + ip + ''; 79 html += '' + host + ''; 80 html += '' + dec + ''; 81 // html += '' + status + ''; 82 //更新状态 83 if (status == '101') { 84 html += ''; 85 } else if (status == '102' || status == '104' || status == '105') { 86 html += ''; 87 } else if (status == '103') { 88 html += ''; 89 } 90 html += '' + ''; 92 html += '' + ''; 94 html += '' + ''; 96 html += ''; 97 } 98 text.append(html); 99 100 }101 //搜索栏102 function searchtomcat() {103 104 var search_val = $('#search_tom').val();105 $.ajax({106 type: "GET",107 url: "/../searchtomcat/",108 data: {'data': search_val},109 datatype: "json",110 success: function (datas) {111 loadtomcatdata(datas);112 $('#preandnext').empty()113 }114 })115 }

index.html

1 2 3 4 {% load static %} 5 自动化运维 6 7 8 9 10 11 12 13 14 15 16 21 22 23

24
25
26

AUTO OPERATION27 自动化运维平台28

29
30
31
32
33 61
62
63 64 65 80
81
82 83
84 85 86

workpage.html

1 2

3
4
5 9
10
11
12 13 14 15 16
17
18
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
Tomcat应用服务器清单
IDIP主机名概要状态检查开启停止
37
38 49
50

这个小项目还会继续做下去,期望能达到当初自己设计的那样。。。。。。。

上一篇:PostgreSQL 库,表,字段,值大小写的问题
下一篇:SAE 助力贵州酒店集团从容支撑贵州特产抢购
相关文章

 发表评论

暂时没有评论,来抢沙发吧~