|
接上一篇博文
ZabbixAPI+django+nginx简单的二次开发实例(一)
步骤二,Django部分
轻量级的网站其实用flask更快,但是我不喜欢造车轮子所以。。。
Django的安装
pip install Django
新建一个project
cd /API/web
django-admin.py startproject PROJECT-NAME
django会在当前路径下新建一个project-name命名的文件夹(我命名为pos)
新建一个app
cd pos/
django-admin.py startapp APP-NAME
django会在当前路径下新建一个app-name命名的文件夹(我命名为moniter)
至此,目录结构为
# tree
-- pos
--- manage.py
--- moniter
---- admin.py
---- admin.pyc
---- apps.py
---- migrations
-----__init__.py
---- __init__.py
---- __init__.pyc
---- models.py
---- models.pyc
---- tests.py
---- views.py
---- views.pyc
--- pos
---- __init__.py
---- __init__.pyc
---- settings.py
---- settings.pyc
---- urls.py
---- urls.pyc
---- wsgi.py
---- wsgi.pyc
修改设定文件
vim pos/setting.py
要修改的地方为
ALLOWED_HOSTS = ['本机的IP', '127.0.0.1', 'localhost'] #允许以这几个IP来连接
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'moniter', #加上刚才创建的app-name
]
STATIC_URL = '/static/'
#加上STATICFILES_DIRS和STATIC_ROOT后面要用
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = 'staticfiles'
新建一个sqlite数据库
vim moniter/model.py
from __future__ import unicode_literals
from django.db import models
# 创建一个叫Pos_moniter的数据表,注意首字母要大写,不然数据导入的时候会报错
class Pos_moniter(models.Model):
#分别用来储存上一篇博文中zabbixAPI抽出来的值,不声明null=True的话默认是notnull的。
name = models.CharField(max_length=50, null=True)
ip = models.CharField(max_length=20)
ping = models.IntegerField(null=True)
diskc = models.DecimalField(max_digits=5, decimal_places=2, null=True)
diskd = models.DecimalField(max_digits=5, decimal_places=2, null=True)
# 在Python3中使用 def __str__(self)
def __unicode__(self):
return self.name
同步数据库
python manage.py makemigrations
python manage.py migrate
在/API/web/pos下面会多出一个db.sqlite3的文件,这就是sqlite的数据库文件
把get_hostinfo.py拷贝过来改造一下
cp /API/web/get_hostinfo.py /API/web/pos/updatedb.py
vim /API/web/pos/updatedb.py
#最上面新导入Django的models
from pos.wsgi import *
from moniter.models import Pos_moniter
class get_hostinfo:
…
#上面都不变,最下面追加更新数据库的模块
def update_sqlite(self,name,ipaddr,ping,diskc,diskd):
global pos_moniter
pos_moniter, created = Pos_moniter.objects.update_or_create(
ip = ipaddr, defaults={
"name": name, "ping": int(ping), "diskc": float(diskc), "diskd": float(diskd)
},
)
def main():
zbx = update_ping()
zbx.login()
names = zbx.get_info("24", "host.get", "name")
ips = zbx.get_info("24", "hostinterface.get", "ip")
pings = zbx.get_info("24", "item.get", "lastvalue", "icmpping")
diskc = zbx.get_info("24", "item.get", "lastvalue", "hrStorageUsage2")
diskd = zbx.get_info("24", "item.get", "lastvalue", "hrStorageUsage3")
for a, b ,c, d, e in zip(names, ips, pings, diskc, diskd):
zbx.update_sqlite(a, b, c, d, e) #把print改成这个
if __name__ == '__main__':
main()
执行一下看看
python /API/web/pos/updatedb.py
进Django的shell模式看看数据在不在
python manage.py shell
>>> from moniter.models import Pos_moniter
>>> Pos_moniter.objects.values_list("name", "ip", "ping", "diskc", "diskd")[:1]
<QuerySet [(u'test\u4e50\u56ed\u5e97\u4e3b\u670d\u52a1\u5668', u'192.168.0.15', 1, Decimal('23.97'), Decimal('44.15'))]>
由于name是中文,这里就变成ACLII码显示了,但是请放心输出到网页上会是正常的中文。
然后把/API/web/pos/updatedb.py挂到crontab里去就能自动更新sqlite的数据啦。
最后做网页
先编辑配置文件
vim /API/web/pos/moniter/views.py
from django.shortcuts import render
from .models import Pos_moniter
# Create your views here.
def home(request):
return render(request, 'home.html', {
'pos': Pos_moniter.objects.all().order_by('name'),
})
vim /API/web/pos/pos/urls.py
from django.conf.urls import url
from django.contrib import admin
from moniter import views as m_views
urlpatterns
= [ url(r
'^admin/', admin.site.urls), url(r
'^home/', m_views.home, name='home'),
]
建个文件夹放网页
mkdir /API/web/pos/moniter/templates
vim /API/web/pos/moniter/templates/home.html
<html>
<body>
<table border="1">
<tr>
<th>NAME</th>
<th>IPADDR</th>
<th>PING</th>
<th>C:Usage</th>
<th>D:Usage</th>
</tr>
{% for i in pos %}
<tr>
<td>{{ i.name }}</td>
<td>{{ i.ip }}</td>
<td>{{ i.ping }}</td>
<td>{{ i.diskc }}</td>
<td>{{ i.diskd }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
运行测试模式
python manage.py runserver 0.0.0.0:8000
用0.0.0.0是为了让本机以外的电脑也能访问
打开网页http://IPADDRESS:8000/home/
就可以看到表格了,但是很丑。。。
用bootstrap稍微美化一下
先建个文件夹
mkdir /API/web/pos/static
从官网下载https://github.com/twbs/bootstrap/releases/download/v3.3.7/bootstrap-3.3.7-dist.zip
解压后把css,fonts,js 这3个文件夹复制到/API/web/pos/static下面
修改一下home.html
vim /API/web/pos/moniter/templates/home.html
{% load staticfiles %} <!--读取staticfiles -->
<html>
<head>
<link> </head>
<body>
<table border="1"> <!--声明表格的class-->
<tr>
<th>NAME</th>
<th>IPADDR</th>
<th>PING</th>
<th>C:Usage</th>
<th>D:Usage</th>
</tr>
{% for i in pos %}
<tr>
<td>{{ i.name }}</td>
<td>{{ i.ip }}</td>
<td>{{ i.ping }}</td>
<td>{{ i.diskc }}</td>
<td>{{ i.diskd }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
再次运行测试模式
python manage.py runserver 0.0.0.0:8000
打开网页http://IPADDRESS:8000/home/
恩,比前面好看多了,还能自适应浏览器大小
接下来Django和Nginx的配合在下一篇博文中讲
ZabbixAPI+django+nginx简单的二次开发实例(三) |
|