django 實現linux運維管理平台


概述

使用django實現一個linux運維管理平台,可以實現注冊登錄,機器管理 ,服務器批量操作,服務器性能監控。

詳細

 

一、准備工作

1、需要准備什么環境

我是在自己的雲服務上編寫和運行的,centos7.2的環境,python是系統自帶的python2.7.5,django安裝的1.8.5版本的。數據庫使用的mysql5.6版本的,rrdtool版本是rrdtool-1.4.8。

2、本例子實現什么功能

①、用戶管理

②、資產信息添加和展示

③、服務器性能監控

④、服務器批量管理

⑤、日志管理

⑥、經驗總結編輯和展示

 

3、django工作原理圖:

原理圖.png

 

4、運維管理平台的功能模塊圖:

功能模塊圖.png

5、功能模塊介紹:

(1)用戶登錄注冊模塊

用戶登錄注冊模塊包含注冊、登錄和注銷登錄的功用。這個模塊主要是負責用戶注冊和登錄運維管理系統,用戶通過點擊注冊按鈕,頁面會跳轉到注冊頁面上,用戶按照頁面的輸入提示框,輸入相應的信息完成注冊,當用戶輸入的用戶已經被注冊了,那么這個時候系統會做出判斷,並且給出用戶提示,如果注冊的用戶,之前並沒有注冊,那么這個時候可以成功注冊,並且會有提示給用戶。這個時候,用戶能夠點擊登錄按鈕跳轉到登錄頁面,使用注冊成功的賬號和密碼完成登錄操作。

(2)Web頁面執行Linux命令

WEBSSH功能模塊是實現在web系統上操作Linux服務器。用戶在首頁面上點擊WEBSSH按鈕頁面會跳轉到WEBSSH命令執行頁面,用戶在命令輸入提示框中輸入格式正確的Linux命令,系統會對用戶給出的命令在服務器上執行,並且把執行輸出時間和執行結果展示出來。這樣方便對Linux服務器進行一些常規操作。

(3)服務器資產信息管理功能

服務器資產信息管理有兩個小的子功能:服務器資產信息后台編輯,用戶可以在后台編輯頁面上對服務器資產信息進行增加修改和刪除操作。服務器信息前端展示,通過把前面通過后台編輯頁面寫入到數據庫里面的信息,再把數據從數據庫里面取出來並格式化輸出給用戶看。

(4)服務器性能監控模塊

服務器性能監控模塊是利用RRDTOOL開源軟件,針對我自己寫的自定義監控腳本,把磁盤、內存、cpu的性能指標數據存入RRDTOOL文件中,然后利用RRDTOOL自身的繪圖功能把性能指標的數據生成圖片,用戶通過訪問相應的按鈕,在后端會執行相應的操作,實時的把1分鍾、3分鍾、5分鍾的數據展示出來。這樣用戶可以查看實時的性能指標。

(5)產品上線模塊

產品上線模塊是針對運維常做的操作而實現的,通過點擊測試環境或者正式環境的發布可以進行程序的更新和回滾操作。這樣可以簡化日常運維工作人員的操作負責性。

(6)服務器批量管理模塊

服務器批量管理模塊可以實現文件的批量同步和程序的批量管理。文件批量同步的話,為了保持大量的服務器的關鍵文件的一致性,所以我們需要的是在一台有主控性的服務器上修改文件,再把文件同步到后端的大批量的服務器上。這樣管理十台、100台、1000台相同業務的服務器的話,事實上跟管理一台服務器是一樣的。再來說一下程序的批量管理,其實和上面的思想相同,前面的文件是為了程序服務的,再者說程序也是文件,當我們把一些文件修改以后進行同步這個時候其實是為了程序做支持工作。我們通過項目來決定不同的主機組然后針對這個主機組做統一的程序管理。

(7)生產經驗總結模塊

生產經驗總結模塊可以分為三部分:文章內容的后台編輯、疑難問題展示、心得體會展示。文章后台編輯的話我真的疑難問題和心得體會各創建一個數據庫表,這樣的話,不管是我針對文章的編輯還是前端 頁面的展示的話都會相對來更好實現和管理。用戶可以把自己在工作中遇到的問題和自己的一些總結針對不同的數據庫表進行編輯,然后通過前端的頁面,把它們展示出來。這樣的話,我們在工作中碰到一定問題的時候可以先從運維管理系統上查一下,看看之前是不是已經有人碰到過這種情況。而且通過別人的總結我們可以學習到更多寶貴的經驗。

(8)日志管理模塊

日志管理模塊針對Linux系統產生的日志和應用程序自身產生的日志進行過濾加工處理操作,因為對我們來說日志包含很多寶貴的信息,我們可以針對我們想要了解的對於海量的日志做處理操作,然后只獲取我們想要的,通過頁面把我們后台處理以后的日志信息展示出來,

二、程序實現

1、用戶登錄

模板(templates)

<body id="login">
  <div class="login-logo">
    <!--a href="index.html">linux 運維管理平台<!--img src="{% static "images/logo.png" %}" alt=""/--></a-->
  </div>
  <h2 class="form-heading"><font size="15" face="Cicle">Linux運維管理平台登錄頁面</font></h2>
  <div class="app-cam">
          {{ nopass  }}{{ deltxt }} 
	  <form action="/login/"  method="post"> {% csrf_token %}
		<input type="text" class="text" value="username" name="username" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'username';}">
		<input type="password" value="password" name="password" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'password';}">
		<div class="submit"><input type="submit" onclick="myFunction()" value="登錄"></div>
		<div class="login-social-link">
          <a href="/login/" class="facebook">
             登錄取消
          </a>
          <a href="/register/" class="twitter">
              用戶注冊
          </a>
        </div>
		<!--ul class="new">
			<li class="new_left"><p><a href="#">忘記密碼</a></p></li>
			<li class="new_right"><p><a href="/register/"> 注冊</a></p></li>
			<div class="clearfix"></div>
		</ul-->
	</form>
  </div>
   <div class="copy_layout login">
      <p>Copyright &copy; 付煒超Linux運維管理系統登錄界面 </p>
   </div>
</body>

視圖(views)

def login(request):
    if request.method == 'POST':
        username = request.POST.get("username","")
        password = request.POST.get("password","")
        user = User.objects.filter(username__exact = username,password__exact = password)
        if user:                                           #如果用戶匹配成功
            response = HttpResponseRedirect('/index/')              #重定向到index
            response.set_cookie('cookie_username',username,36)    #設置cookie 
            return response                                         #把index頁面輸出   
        else:
            nopass="用戶名或者密碼輸入錯誤"                        #沒有匹配成功  
            return render(request,'login.html',{'nopass':nopass})
        return HttpResponse('yes')
    return render(request,'login.html')

模型(models)

class User(models.Model):
    username = models.CharField(max_length=30)
    password = models.CharField(max_length=30)

    def __unicode__(self):
        return self.username

路由(url)

url(r'^login/$', 'monitor.views.login'),

2、用戶注冊

模板(templates)

<body id="login">
  <div class="login-logo">
    <!--a href="index.html"><img src="{%static "images/logo.png"%}" alt=""/></a-->
  </div>
  <h2 class="form-heading"><font size="15" face="Cicle">Linux運維管理平台注冊頁面</font> </h2>
  <form class="form-signin app-cam" action="/register/" method="post">{% csrf_token %}
       <p> {{ registusername }} {{ registered }}</p>
       <p> {{ registAdd }} {{ Registered }}</p>
       <p> 請輸入您的想要注冊的賬號和密碼</p>
      <input type="text" class="form-control1" placeholder="username" name="username" autofocus="">
      <input type="password" class="form-control1" placeholder="password" name="password" >
      <label class="checkbox-custom check-success">
      </label>
      <button class="btn btn-lg btn-success1 btn-block" type="submit">Submit</button>
      <div class="registration">
          Already Registered.
          <a class="" href="/login/">
              Login
          </a>
      </div>
  </form>
   <div class="copy_layout login register">
      <p>Copyright &copy; 付煒超運維管理系統的注冊頁 </p>
   </div>
</body>

視圖(views)

def register(request):
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        try:
             registusername = User.objects.filter(username=username).get().username
             registered="已經注冊了"
             return render(request,'register.html',{'registusername':registusername,'registered':registered})
        except:
            registAdd = User.objects.create(username=username,password=password)
            Registered="注冊成功!!!"
            return render(request,'register.html',{'registAdd':registAdd,'Registered':Registered})
    else:
        return render(request,'register.html')
    return render(request,'register.html')

3、用戶注銷

視圖(views)

def logout(request):
    return render(request,'login.html')
    response.delete_cookie('cookie_username')                            #刪除cookie_username對應的用戶的cookie 
    return  response

4、服務器資產信息

視圖(templates)

def infor(request):
    infor_list = Information.objects.all()
    return render_to_response('information.html',{'infor_list':infor_list})

模型(models)

from django.db import models
class Information(models.Model):
    name = models.CharField(max_length=30)
    privateip = models.GenericIPAddressField()
    publicip = models.GenericIPAddressField()
    use = models.TextField()
    zoneid = models.CharField(max_length=30)
    cpu = models.CharField(max_length=50)
    memory = models.CharField(max_length=50)
    datadisk = models.CharField(max_length=30)
    time = models.DateTimeField()

    def __unicode__(self):
        return self.name

admin

class InformationAdmin(admin.ModelAdmin):
    list_display = ('name','privateip','publicip','use','zoneid','cpu','memory','datadisk')
    
admin.site.register(Information,InformationAdmin)

模板(templates)

{% block content %}
<table border="1">
<tr>
<th>服務器主機名</th>
<th>服務器內網ip</th>
<th>服務器公網ip</th>
<th>服務器zoneid</th>
<th>服務器cpu個數</th>
<th>服務器內存大小</th>
<th>服務器數據盤大小</th>
<th>服務器信息記錄時間</th>
<th>服務器用途</th>
</tr>

{% for infor in infor_list %}
<tr>
<td>{{ infor.name }}</td> 
<td>{{ infor.privateip}} </td>  
<td>{{ infor.publicip}} </td>  
<td>{{ infor.zoneid}} </td>  
<td>{{ infor.cpu}} </td>  
<td>{{ infor.memory}} </td>  
<td>{{ infor.datadisk}} </td>  
<td>{{ infor.time}} </td>  
<td> {{ infor.use}}</td>

</tr>

{% endfor %}
{% endblock %}

5、服務器性能監控

視圖(views)

def servers(request):
    if request.method == 'POST':
        hostgroup = request.POST.get("hostgroup","")
        model = request.POST.get("model","")
        user = request.POST.get("user","")
        command = request.POST.get("command","")
        os.environ['hostgroup']=str(hostgroup)
        os.environ['model']=str(model)
        os.environ['user']=str(user)
        os.environ['command']=str(command)
        output = commands.getoutput("sh /home/zqxt_form2/monitor/ansible.sh $hostgroup $model $user $command")
        return render(request,'servers.html',{'output':output})
    return render(request,'servers.html')

模板(templates)

<section id="content">
	<div class="zerogrid">
		<div class="row block">
			<div class="main-content">
				<article class="col-1-3">
					<div class="wrap">
						<div class="heading">
							<img src="{% static "jpg/monitor/cpu1.jpg" %}"/>   
							<h2><a href="#">cpu 1 分鍾的性能圖</a></h2>
						</div>
						
					</div>
				</article>
				<article class="col-1-3">
					<div class="wrap">
						<div class="heading">
							<img src="{% static "jpg/monitor/cpu3.jpg" %}"/>
							<h2><a href="#">cpu 3 分鍾的性能圖</a></h2>
						</div>
						
					</div>
				</article>
				<article class="col-1-3">
					<div class="wrap">
						<div class="heading">
							<img src="{% static "jpg/monitor/cpu5.jpg" %}"/>
							<h2><a href="#">cpu 5 分鍾的性能圖</a></h2>
						</div>
						
					</div>
				</article>
				<article class="col-1-3">
					<div class="wrap">
						<div class="heading">
							<img src="{% static "jpg/monitor/memory1.jpg" %}"/>
							<h2><a href="#">內存 1 分鍾的使用圖</a></h2>
						</div>
						
					</div>
				</article>
				<article class="col-1-3">
					<div class="wrap">
						<div class="heading">
							<img src="{% static "jpg/monitor/memory3.jpg" %}"/>
							<h2><a href="#">內存 3 分鍾的使用圖</a></h2>
						</div>
						
					</div>
				</article>
				<article class="col-1-3">
					<div class="wrap">
						<div class="heading">
							<img src="{% static "jpg/monitor/memory5.jpg" %}"/>
							<h2><a href="#">內存 5 分鍾的使用圖</a></h2>
						</div>
						
					</div>
				</article>
				<article class="col-1-3">
					<div class="wrap">
						<div class="heading">
							<img src="{% static "jpg/monitor/disk1.jpg" %}"/>
							<h2><a href="#">磁盤 1 分鍾的空閑量</a></h2>
						</div>
						
					</div>
				</article>
				<article class="col-1-3">
					<div class="wrap">
						<div class="heading">
							<img src="{% static "jpg/monitor/disk3.jpg" %}"/>
							<h2><a href="#">磁盤 3 分鍾的空閑量</a></h2>
						</div>
						
					</div>
				</article>
				<article class="col-1-3">
					<div class="wrap">
						<div class="heading">
							<img src="{% static "jpg/monitor/disk5.jpg" %}"/>
							<h2><a href="#">磁盤 5 分鍾的空閑量</a></h2>
						</div>
						
					</div>
				</article>
			</div>
			
		</div>
	</div>
</section>

rrdtool.sh

#!/bin/bash
#rrdtool create cpu.rrd --step 5 DS:cpuds:GAUGE:8:0:U   RRA:AVERAGE:0.5:1:17280  RRA:MIN:0.5:1:17280  RRA:MAX:0.5:1:17280 RRA:AVERAGE:0.5:10:3456 RRA:MIN:0.5:10:3456 RRA:MAX:0.5:10:3456  RRA:AVERAGE:0.5:100:1210  RRA:MIN:0.5:100:1210 RRA:MAX:0.5:100:1210
while true;do 
    cpu=`vmstat 1 1 |tail -n 1 |awk  '{print $15}'`
    memory=`free -m |grep "Mem" |awk '{print $4+$6}'`
    disk=`df -h |head -n 2 |tail -n 1 |awk '{print $5}'|awk -F '%' '{print $1}'`
    rrdtool update ./cpu.rrd N:${cpu}
    rrdtool update ./memory.rrd N:${memory}
    rrdtool update ./disk.rrd N:${disk}
    sleep 5
done

#1minute=`date --date '1 minute ago ' +%s`
#3minute=`date --date '3 minute ago ' +%s`
#5minute=`date --date '5 minute ago ' +%s`

#rrdtool graph cpu1.jpg --step 5 -s ${1minute} -t "cpu  1 minute monitor" -v cpu DEF:cpu=./cpu.rrd:cpuds:AVERAGE LINE1:cpu#FF0000:'cpu avg'
#rrdtool graph cpu3.jpg --step 5 -s ${3minute} -t "cpu  3 minute monitor" -v cpu DEF:cpu=./cpu.rrd:cpuds:AVERAGE LINE1:cpu#FF0000:'cpu avg'
#rrdtool graph cpu5.jpg --step 5 -s ${5minute} -t "cpu  5 minute monitor" -v cpu DEF:cpu=./cpu.rrd:cpuds:AVERAGE LINE1:cpu#FF0000:'cpu avg'

六、服務器批量管理

視圖(models)

def servers(request):
    if request.method == 'POST':
        hostgroup = request.POST.get("hostgroup","")
        model = request.POST.get("model","")
        user = request.POST.get("user","")
        command = request.POST.get("command","")
        os.environ['hostgroup']=str(hostgroup)
        os.environ['model']=str(model)
        os.environ['user']=str(user)
        os.environ['command']=str(command)
        output = commands.getoutput("sh /home/zqxt_form2/monitor/ansible.sh $hostgroup $model $user $command")
        return render(request,'servers.html',{'output':output})
    return render(request,'servers.html')

模板(templates)

	<h1>批量管理頁面</h1>
	<div class="login-01">
			<form action="/servers/"  method="post"> {% csrf_token %}
				<ul>
				<li class="first">
					<a href="#" class=" icon email"></a><input type="text" class="text" value="主機組" name="hostgroup"  onFocus="this.value = '';" onBlur="if (this.value == '') {this.value = '主機組';}" >
					<div class="clear"></div>
				</li>
                                <li class="first">
                                        <a href="#" class=" icon email"></a><input type="text" class="text" value="模塊名" name="model" onFocus="this.value = '';" onBlur="if (this.value == '') {this.value = '模塊名';}" >
                                        <div class="clear"></div>
                                </li>

				<li class="first">
					<a href="#" class=" icon email"></a><input type="text" class="text" value="用戶" name="user" onFocus="this.value = '';" onBlur="if (this.value == '') {this.value = '用戶';}" >
					<div class="clear"></div>
				</li>
				<li class="first">
					<a href="#" class=" icon phone"></a><input type="text" class="text" value="命令" name="command" onFocus="this.value = '';" onBlur="if (this.value == '') {this.value = '命令';}" >
					<div class="clear"></div>
				</li>
				<!--li class="second">
				<a href="#" class=" icon msg"></a><textarea value="Message" onFocus="this.value = '';" onBlur="if (this.value == '') {this.value = 'Comments';}">{{ output}}/textarea>
				<div class="clear"></div>
				</li-->
			</ul>
			<input type="submit" onClick="myFunction()" value="Submit" >
			<div class="clear"></div>
		</form>
   </div>   
   <div class="login-01">
         <form>
		     <li class="second">
				<a href="#" class=" icon phone"></a><textarea value="Message" onFocus="this.value = '';" onBlur="if (this.value == '') {this.value = '命令輸出結果';}">{{ output }}</textarea>
				<div class="clear"></div>
		     </li>
		 </form>
	</div>

ansible.sh

#!/bin/bash
ansible  $1 -m $2  -a "sudo su - '$3' -c '$4'"

 

 

三、運行效果

程序運行:python manage.py runserver 0.0.0.0:8080

訪問:http:/你服務器的ip:8080/

效果圖展示:

注冊頁面:

注冊.png

登錄:登錄.png

首頁面:

首頁01.png

首頁02.png資產編輯:

資產編輯.png

監控頁面:

監控頁面.png

 

服務器批量管理:

批量管理.png

四、文件截圖

blob.png

 

五、其他補充

1、用於生產環境有哪些需要改善的地方

django需要配置nginx和uwsgi進行部署

 

 

注:本文著作權歸作者,由demo大師發表,拒絕轉載,轉載需要作者授權


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM