⾃动化运维平台搭建
项⽬简介:
项⽬介绍:⾃动化运维是未来的趋势,最近学了不少东西,正好通过这个⼩项⽬把这些学的东西串起来,练练⼿。 基础架构:
服务器端: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(){
13echo `date +"%F %T"`""$* >> /logs/tomcat.log
14 }
15
16 #开启tomcat函数
17 starttomcat(){
18 log "[command]:"$0" [parameters]:"$*
19 #启动tomcat前需要调⽤checktomcat获取tomcat状态
20 status=`. /operation/tomcat/checktomcat.sh $1 $2`
21 #如果tomcat处于运⾏状态,就不必再启动,把状态信息返回前端
22if [ $status -eq 101 ];then
23echo101
24 #如果tomcat未运⾏,执⾏启动操作
25else
26su - tomcat -c $1/bin/startup.sh >/dev/null2>&1
27 #设置count计时
28 count=0
29 #每5秒检查⼀次tomcat状态,直到检查到tomcat成功启动或者超时
30 status=`. /operation/tomcat/checktomcat.sh $1 $2`
31until [ $status -eq 101 ] || [ $count -ge $3 ]
32do
33sleep5
34 let count=$count+5
35 status=`. /operation/tomcat/checktomcat.sh $1 $2`
36done
37 #如果检测到tomcat正常,判断为启动成功,返回状态信息
38if [ $status -eq 101 ];then
39echo101
40 #如果超时还未启动成功,则判断为启动超时,返回启动超时代号104 41else
42echo104
43fi
44fi
45 }
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现在运⾏状态
8def operation(request):
9# 获得前台信息
10 tomcat_id = ('id')
11 tomcat_action = ('action')
12 oper = ('loginname')
13# 根据ID和action 获得任务信息,并形成完整的操作SQL,都存⼊taskinfo中
14 taskinfo = get_taskinfo(tomcat_id, tomcat_action, oper)
15# 传⼊taskinfo,执⾏SQL操作,返回⽬标服务器控制台的结果
16 mytask = Task(taskinfo)
17 result = ute()
18if result.isdigit():
19 taskinfo['resulut'] = result
20else:
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 }
32return JsonResponse({
33'status': taskinfo['resulut'],
34'message': message[taskinfo['resulut']],
35 })
36
37# 根据ID⽣成taskinfo
38def get_taskinfo(tomcat_id, tomcat_action, oper):
39 with connection.cursor() as cursor:
40 ute(
41'SELECT id, tomcatport, tomcathome, ipaddress, startwait, stopwait FROM tomcatdata WHERE id = %s' % tomcat_id
42 )
43 tomcater = dictfetchall(cursor)[0]
44 serverip = tomcater['ipaddress']
45 ute(
46"SELECT user1,password1 FROM machine_pwd WHERE ipaddress = '%s'" % serverip
47 )
48 userinfo = dictfetchall(cursor)[0]
49if 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)
53elif 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)
60elif 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 }
77return task_info
78
79
80# 写⼊操作记录并更新tomcat状态
81def 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 ute(sqlstatement1)
87 ute(sqlstatement2)
Task类
1import paramiko
2
3
4class Task(object):
5def__init__(self, task_info):
6 self.task_info = task_info
7
8def create_connect(self):
9# 创建SSH对象
10 connectObj = paramiko.SSHClient()
11# 把要连接的机器添加到known_hosts⽂件中
12 connectObj.set_missing_host_key_policy(paramiko.AutoAddPolicy())
13# 连接服务器
14 t(
15 hostname=self.task_info['ip'],
16 username=self.task_info['user'],
17 password=self.task_info['pwd'],
18 port=22,
19 timeout=10
20 )
21# 判断是否连接成功
22>>>####
23return connectObj
24
25def execute(self):
26try:
27 connectObj = ate_connect()
28except:
29return'102'
30 cmd = self.task_info['cmd']
31 stdin, stdout, stderr = _command(cmd)
32 result = ad()
33 connectObj.close()
34if not result:
jquery框架搭建35 result = ad().strip()
36return result.decode().strip()
js⽂件
1 $(function () {
2 $("ul[id='servicemgr'] li").click(function () {
3 <!-- 导⼊workPage-->
4if (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)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
前端开发工作目标
« 上一篇
使用Bootstrap快速构建响应式网页的方法
下一篇 »
发表评论