Rohmad Hidayat's Blog

Icon

My Notes on Mobile Technology, Python GUI, Python Web Framework, Software Development, Database, Server, Embedded System, Mini Computer

Simple Network Host Monitor

hostmonitor.jpg

Setelah membaca sedikit tentang bagaimana python berinteraksi dengan sistem, terlintas ide untuk membikin simple host monitor dengan memanfaatkan command ‘ping’. Nah, sekalian untuk mengetes kemampuan interaksi cherrypy+python dengan sistem ini langsung saja saya tulis aplikasi berikut ini:

==========python code

import cherrypy
import os
from cherrytemplate import renderTemplate

curdir = os.path.join(os.getcwd(), os.path.dirname(__file__))

class Pages:
def index(self, page=’home’, no = None, ip=’192.168.0.254′,cmd=’ping -c 4′,ip1=’10.13.1.254′,ip2=’172.16.30.7′):
if no is None:
ip = ‘192.168.0.5’
cmd = os.popen(“”+str(cmd)+” “+str(ip)+””,”r”)
lines = cmd.readlines()
cmd1 = os.popen(“ping -c 4 10.13.1.254″,”r”)
lines1 = cmd1.readlines()
cmd2 = os.popen(“ping -c 4 172.16.30.7″,”r”)
lines2 = cmd2.readlines()
return renderTemplate(file=’ping.html’)

root = Pages()

conf = {

‘/static’: {
‘tools.staticdir.on’: True,
‘tools.staticdir.dir’: ‘images’,
‘tools.staticdir.root’: curdir,
}
}

cherrypy.tree.mount(root, config=conf)
cherrypy.config.update(‘pingtutorial.conf’)
cherrypy.server.quickstart()
cherrypy.engine.start()

=======================

karena cherrypy menganut ‘aliran’ mvc yang memisahkan antara logic code dengan tampilan maka bentuk tampilan di web dibuat terpisah kemudian diberi nama dengan ping.html ( sesuai di code renderTemplate() ).

berikut saya buat html dengan microsoft frontpage (biar mudah saja).

========html presentation ping.html

<html>
<head>
<meta http-equiv=”refresh” content=”30″>
<img src=”../static/virus.jpg” width=”80″> Coded With Python, Cherrypy, CherryTemplates Full MVC architecture<br><center><h2>Status Koneksi Internet PSEKP UGM</h2></center>
</head>
<body background=”../static/dirback.jpg”>
<form method=GET action=../>
<table border=”1″ cellpadding=”0″ cellspacing=”0″ style=”border-collapse: collapse” bordercolor=”#111111″ width=”100%” id=”AutoNumber1″>
<tr>
<td width=”33%” align=”center” bgcolor=”#9999FF”><b>
<font face=”Trebuchet MS”>Host : <py-eval=”ip”></font></b></td>
<td width=”33%” align=”center” bgcolor=”#9999FF”><b>
<font face=”Trebuchet MS”>Host: switch BIOTEK</font></b></td>
<td width=”34%” align=”center” bgcolor=”#9999FF”><b>
<font face=”Trebuchet MS”>Host: DNS UGM</font></b></td>
</tr>
<tr>
<td width=”33%” valign=”top”> <font face=”Arial Narrow” size=”1″>
<py-for=”line in lines”>
<py-eval=”line”><br>
</py-for>
<py-if=”str(len(lines)) > ‘4’ “></font>
<h3><font color=”green”>Host <py-eval=”ip”> Aktif</font></h3>
</py-if>
<py-else>
<h3><font color=”red”>Host <py-eval=”ip”> Mati</font></h3>
</py-else>

</td>
<td width=”33%” valign=”top”> <font face=”Arial Narrow” size=”1″>

<py-for=”line in lines1″>
<py-eval=”line”><br>
</py-for>
<py-if=”str(len(lines1)) > ‘4’ “></font>
<h3><font color=”green”>Host <py-eval=”ip1″> Aktif</font></h3>
</py-if>
<py-else>
<h3><font color=”red”>Host <py-eval=”ip1″> Mati</font></h3>
</py-else>

</td>
<td width=”34%” valign=”top”> <font face=”Arial Narrow” size=”1″>

<py-for=”line in lines2″>
<py-eval=”line”><br>
</py-for>
<py-if=”str(len(lines2)) > ‘4’ “></font>
<h3><font color=”green”>Host <py-eval=”ip2″> Aktif</font></h3>
</py-if>
<py-else>
<h3><font color=”red”>Host <py-eval=”ip2″> Mati</font></h3>
</py-else>

</td>
</tr>
</table>
<font face=”Arial Narrow”><b>Arah Koneksi:</b></font><p><b>
<font face=”Arial Narrow”>PSEKP UGM NETgate
——————————————–>Switch di Bioteknologi——————————————->
UGM</font></b><br><br>
Host: <input type=text name=ip size=”20″><br>
Command :<input typr=text name=cmd size=”20″><br>
<input type=submit value=”CEK Host”> </p>
</form>
</body>
</html>

========

Contoh hasil aplikasi ini dapat dicoba di http://psekp.ugm.ac.id:8088

Advertisements

Filed under: Computer, Programming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: