summaryrefslogtreecommitdiff
path: root/tasks/snmp_agent_uptime
diff options
context:
space:
mode:
Diffstat (limited to 'tasks/snmp_agent_uptime')
-rw-r--r--tasks/snmp_agent_uptime/howtos/en/index.html319
-rw-r--r--tasks/snmp_agent_uptime/howtos/images/01.pngbin0 -> 67779 bytes
-rw-r--r--tasks/snmp_agent_uptime/howtos/si/index.html308
-rw-r--r--tasks/snmp_agent_uptime/task.py224
4 files changed, 851 insertions, 0 deletions
diff --git a/tasks/snmp_agent_uptime/howtos/en/index.html b/tasks/snmp_agent_uptime/howtos/en/index.html
new file mode 100644
index 0000000..bef6792
--- /dev/null
+++ b/tasks/snmp_agent_uptime/howtos/en/index.html
@@ -0,0 +1,319 @@
+<html>
+ <head>
+ <meta http-equiv="content-type" content="text/html; charset=utf-8">
+ <title>snmp_agent_uptime</title>
+ </head>
+ <body>
+ <h1>snmp_agent_uptime</h1>
+ <h2>Quick instructions</h2>
+ <p>
+ Set up three virtual computers, SimpleArbiter with the disk image
+ simpleArbiterDhcp, SNMPServer and SNMPClient. Write a program
+ called upminutes. This program should output the uptime of the
+ computer in minutes. Set it up on SNMPClient in the home directory
+ of the user test with the password test.
+ </p>
+ <p>
+ Make sure that the SNMP server reports it's uptime in seconds
+ over SNMP under NET-SNMP-EXTEND-MIB::nsExtendOutput2Table.
+ </p>
+ <p>
+ Write a script called beri.sh that reads the value from the
+ OID 1.3.6.1.4.1.8072.1.3.2.4.1.2 on SNMPServer. Set it up on
+ SNMPClient in the home directory of the user test.
+ </p>
+ <h2>Instructions</h2>
+ <h3>Set up of VM VirtualBox</h3>
+ <ol>
+ <li>
+ Download the following virtual images (*.vid) from the directory
+ with images of virtual computers:
+ <ol type="disc">
+ <li>
+ simpleArbiterDhcp.vdi
+ </li>
+ <li>
+ some-bash-console.vdi – twice, one for SNMPServer
+ and second for SNMPClient
+ </li>
+ </ol>
+ </li>
+ <li>
+ VM VirtualBox WARNING! If you want to use one same virtual image
+ (some-bash-console.vdi) for two virtual computers
+ (SNMPServer and SNMPClient), you must <strong>change UUID</strong>
+ of one image.
+ <ol type="disc">
+ <li>
+ Use this command
+ <i>vboxmanage internalcommands sethduuid name-of-disk.vdi</i>
+ to change UUID (<a href="http://www.giannistsakiris.com/2009/05/06/virtualbox-how-to-change-the-uuid-of-virtual-disk-vdi/">hint</a>).
+ </li>
+ </ol>
+ </li>
+ <li>
+ Final view of sets VM VirtualBox machines.<br>
+ <img src="../images/01.png" alt="VM VirtualBox machines" width="800">
+ </li>
+ </ol>
+
+ <h3>Set up of SNMPServer machine</h3>
+ <ol>
+ <li>
+ setup network as “Bridged Adapter” -> Machine-> Settings ->Network
+
+ Install snmpd and snmp packages and tools for inspecting the
+ data available over SNMP.
+ <ol type="disc">
+ <li>
+ command <i>apt-get install snmpd snmp snmp-mibs-downloader</i>
+ </li>
+ </ol>
+ <li>
+ RECOMMENDATION! Before doing any changes to your /etc/snmp/snmpd.conf
+ file take a copy of original file.
+ <ol type="disc">
+ <li>
+ command <i>cp /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf.orig</i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ Set up the snmp server to allow all other computers to access it = edit
+ snmpd.conf file.
+ <ol type="disc">
+ <li>
+ command <i>nano /etc/snmp/snmpd.conf</i>
+ (you can use different editor)
+ </li>
+ <li>
+ Check this 4 rows and make sure they look like this:<br>
+ <i># Listen for connections from the local system only<br>
+ # agentAddress udp:127.0.0.1:161<br>
+ # Listen for connections on all interfaces (both IPv4 *and* IPv6)<br>
+ agentAddress udp:161,udp6:[::1]:161</i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ Add a group (community) of computers we deem to be worthy of
+ accessing all data. This group will be called students = again edit
+ snmpd.conf file.
+ <ol type="disc">
+ <li>
+ rocommunity students 0.0.0.0/0
+ </li>
+ <li>
+ change 0.0.0.0./0 into correct address <i>ifconfig -a</i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ OPTIONAL CONFIGURATION. Lower in the same file you can set the
+ location of the computer snmpd is running on and the name
+ of the administrator.
+ <ol type="disc">
+ <li>
+ find under # SYSTEM INFORMATION
+ </li>
+ </ol>
+ </li>
+ <li>
+ Make sure that the SNMP server reports it's uptime in seconds
+ over SNMP under NET-SNMP-EXTEND-MIB::nsExtendOutput2Table.
+ <ol type="disc">
+ <li>
+ create script upseconds containing this and save it where
+ you want:<br>
+ <i>#!/bin/bash<br>
+ uptime=$(&lt;/proc/uptime)<br>
+ seconds=${uptime%%.*}<br>
+ echo "Uptime in seconds:" $seconds<br>
+ exit 0</i>
+ </li>
+ <li>
+ don't forget to make the script runnable:<br>
+ command <i>chmod +x /your_path_to_script/upseconds</i>
+ </li>
+ <li>
+ Then edit file <i>snmpd.conf</i> and under # EXTENDING THE AGENT
+ comment all three tests and add your line of your code
+ with upsecond script. It will look like this:<br>
+ <i># extend test1 /bin/echo Hello, world!<br>
+ # extend-sh test2 echo Hello, world! ; echo Hi there ; exit 35<br>
+ # extend-sh test3 /bin/sh /tmp/shtest<br>
+ extend-sh "your_name" "your_path_to_script_upseconds"
+ </i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ You need to restart the snmp services.
+ <ol type="disc">
+ <li>
+ command <i>/etc/init.d/snmpd restart</i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ You can test your configuration through localhost.
+ <ol type="disc">
+ <li>
+ command <i>snmpwalk localhost -c public -v1</i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ Also test the correct return of server's uptime in seconds SNMP under
+ NET-SNMP-EXTEND-MIB::nsExtendOutput2Table.
+ <ol type="disc">
+ <li>
+ command <i>snmpwalk -c students -v1 IPaddressOfServer
+ 1.3.6.1.4.1.8072.1.3.2.4.1.2</i>
+ </li>
+ <li>
+ you should get string: "Uptime in seconds: xyz"
+ </li>
+ </ol>
+ </li>
+ </ol>
+
+ <h3>Set up of SNMPClient machine</h3>
+ <ol>
+ <li>
+ Install snmpd and snmp packages.
+ <ol type="disc">
+ <li>
+ command <i>apt-get install snmpd snmp</i>
+ </li>
+ </ol>
+ <li>
+ RECOMMENDATION! Before doing any changes to your /etc/snmp/snmpd.conf
+ file take a copy of original file.
+ <ol type="disc">
+ <li>
+ command <i>cp /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf.orig</i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ Set up the snmp client to allow all other computers to access it = edit
+ snmpd.conf file.
+ <ol type="disc">
+ <li>
+ command <i>nano /etc/snmp/snmpd.conf</i>
+ (you can use different editor)
+ </li>
+ <li>
+ (un)comment these four lines as below:<br>
+ <i># Listen for connections from the local system only<br>
+ # agentAddress udp:127.0.0.1:161<br>
+ # Listen for connections on all interfaces (both IPv4 *and* IPv6)<br>
+ agentAddress udp:161,udp6:[::1]:161</i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ Add a group (community) of computers we deem to be worthy of
+ accessing all data. This group will be called students = again edit
+ snmpd.conf file.
+ <ol type="disc">
+ <li>
+ rocommunity students 0.0.0.0/0
+ </li>
+ <li>
+ write correct network address = command <i>ifconfig -a</i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ OPTIONAL CONFIGURATION. Lower in the same file you can set the
+ location of the computer snmpd is running on and the name
+ of the administrator.
+ <ol type="disc">
+ <li>
+ find under # SYSTEM INFORMATION
+ </li>
+ </ol>
+ </li>
+
+ <li>
+ You need to restart the snmp services.
+ <ol type="disc">
+ <li>
+ command <i>/etc/init.d/snmpd restart</i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ You can test your configuration through localhost.
+ <ol type="disc">
+ <li>
+ command <i>snmpwalk localhost -c public -v1</i>
+ </li>
+ </ol>
+ </li>
+ <li>Create the user <i>test</i> with password <i>test</i>
+ <ol type="disc">
+ <li>
+ command <i>adduser test</i>
+ </li>
+ </ol>
+ </li>
+ <li>Login as user <i>test</i> and create program <i>upminutes</i>
+ in the home directory. This program should output the uptime of the
+ computer in minutes.
+ <ol type="disc">
+ <li>
+ command <i>nano upminutes</i> (you can use different editor)
+ </li>
+ <li>
+ add the source code:<br>
+ <i>#!/bin/bash<br>
+ uptime=$(&lt;/proc/uptime)<br>
+ uptime=${uptime%%.*}<br>
+ minutes=$(( uptime / 60 ))<br>
+ echo $minutes<br>
+ exit 0</i>
+ </li>
+ <li>
+ don't forget to make the program runnable:<br>
+ command <i>chmod +x /home/test/upminutes</i>
+ </li>
+ <li>
+ test the program (it should output the uptime of the
+ computer in minutes)<br>
+ command <i>/home/test/upminutes</i>
+ </li>
+ </ol>
+ </li>
+ <li><strong> As user <i>test</i> and create next script called
+ <i>beri.sh</i> that reads the value from the OID
+ 1.3.6.1.4.1.8072.1.3.2.4.1.2. on SNMPServer. Set it up on SNMPClient
+ in the home directory of the user test.
+ <ol type="disc">
+ <li>
+ command <i>nano beri.sh</i> (you can use different editor)
+ </li>
+ <li>
+ add the source code:<br>
+ <i>#!/bin/bash<br>
+ snmpwalk -c students -v1 IPServerAddress 1.3.6.1.4.1.8072.1.3.2.4.1.2<br>
+ exit 0</i>
+ </li>
+ <li>
+ don't forget to make the program runnable:<br>
+ command <i>chmod +x /home/test/upminutes</i>
+ you can test it with <i>./beri.sh</i>
+ </li>
+ </ol>
+ </li>
+ </ol>
+
+ <h3> Setting SimpleArbiter </h3>
+ User: tester
+ Password: test
+
+ apt-get install libsnmp-python
+ </body>
+</html>
diff --git a/tasks/snmp_agent_uptime/howtos/images/01.png b/tasks/snmp_agent_uptime/howtos/images/01.png
new file mode 100644
index 0000000..1bd01aa
--- /dev/null
+++ b/tasks/snmp_agent_uptime/howtos/images/01.png
Binary files differ
diff --git a/tasks/snmp_agent_uptime/howtos/si/index.html b/tasks/snmp_agent_uptime/howtos/si/index.html
new file mode 100644
index 0000000..f490f6c
--- /dev/null
+++ b/tasks/snmp_agent_uptime/howtos/si/index.html
@@ -0,0 +1,308 @@
+<html>
+ <head>
+ <meta http-equiv="content-type" content="text/html; charset=utf-8">
+ <title>snmp_agent_uptime</title>
+ </head>
+ <body>
+ <h1>snmp_agent_uptime</h1>
+ <h2>Namen naloge</h2>
+ <p>
+
+ </p>
+ <h2>Hitra navodila</h2>
+ <p>
+ Naloga: Postavi tri navidezne racunalnike SimpleArbiter s sliko diska simpleArbiterDhcp,SNMPServer in
+ SNMPClient.
+ Napiši program upminutes, ki bo izpisal v minutah koliko casa je racunalnik vklopljen.
+ Postavi ga na SNMPClient v domaÄŤi imenik uporabnika test z geslom test.
+ </p>
+ <p>
+ Poskrbi, da bo SNMP strežnik prek SNMP pod NET-SNMP-EXTEND-MIB::nsExtendOutput2Table sporočal, koliko časa je vklopljen v sekundah.
+ </p>
+ <p>
+ Napisi skripto, poimenovano beri.sh, ki prek SNMP prebere vrednost s streĹľnika
+ SNMPServer na OID 1.3.6.1.4.1.8072.1.3.2.4.1.4.
+ Postavi jo na SNMP klienta, v domači imenik uporabnika test z geslom test.
+ </p>
+ <h2>Navodila</h2>
+ <h3>Nastavitev VM VirtualBox-a</h3>
+ <ol>
+ <li>
+ Prenesi sledeče slike virtualk (*.vid) iz datoteke
+ z slikami virtualk računalnikov:
+ <ol type="disc">
+ <li>
+ simpleArbiterDhcp.vdi
+ </li>
+ <li>
+ neko-bash-konzolo.vdi (dvakrat), enkrat za SNMPServer
+ in drugič SNMPClient.
+ </li>
+ </ol>
+ </li>
+ <li>
+ VM VirtualBox OPOZORILO! Ce hoces uporabljati isto sliko virtualke
+ (neko-bash-konzolo.vdi) za dva navidezna racunalnika
+ (SNMPServer in SNMPClient), moras <strong>spremeniti UUID</strong>
+ ene od slik.
+ <ol type="disc">
+ <li>
+ Uporabi ta ukaz
+ <i>vboxmanage internalcommands sethduuid ime-diska.vdi</i>
+ za spreminjanje UUID (<a href="http://www.giannistsakiris.com/2009/05/06/virtualbox-how-to-change-the-uuid-of-virtual-disk-vdi/">namig</a>).
+ </li>
+ </ol>
+ </li>
+ <li>
+ Primer VM VirtualBox-a po nastavitvi.<br>
+ <img src="..\images\01.png" width="800">
+ </li>
+ </ol>
+
+ <h3>Nastavitev SNMPServer virtualke</h3>
+ <ol>
+ <li>
+ Nasnemi snmpd and snmp orodja za pregledovanje
+ podatkov, ki so no voljo preko SNMP.
+ <ol type="disc">
+ <li>
+ ukaz <i>apt-get install snmpd snmp snmp-mibs-downloader</i>
+ </li>
+ </ol>
+ <li>
+ PRIPOROCILO! Preden spreminjate vaso datoteko /etc/snmp/snmpd.conf,
+ naredite kopijo originalne datoteke.
+ <ol type="disc">
+ <li>
+ ukaz <i>cp /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf.orig</i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ Nastavi snmp streznik tako, da se bodo lahko nanj povezali drugi racunalniki
+ (popravi datoteko snmpd.conf).
+ <ol type="disc">
+ <li>
+ ukaz <i>nano /etc/snmp/snmpd.conf</i>
+ (lahko uporabis drug urejevalnik besedila)
+ </li>
+ <li>
+ odkomentiraj sledece stiri vrstice:<br>
+ <i># Listen for connections from the local system only<br>
+ # agentAddress udp:127.0.0.1:161<br>
+ # Listen for connections on all interfaces (both IPv4 *and* IPv6)<br>
+ agentAddress udp:161,udp6:[::1]:161</i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ Dodaj skupino (community) racunalnikov, ki lahko dostopajo do vseh podatkov.
+ To skupino bomo poimenovali students (spet potrebno spremeniti datoteko)
+ snmpd.conf file.
+ <ol type="disc">
+ <li>
+ rocommunity students 0.0.0.0/0
+ </li>
+ <li>
+ napisi pravilni naslov omrezja = ukaz <i>ifconfig -a</i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ DODATNA (NEOBVEZNA) KONFIGURACIJA. Nizje v isti datoteki lahko nastavis
+ lokacijo racunalnika, na katerem deluje snmp, ter ime administratorja.
+ <ol type="disc">
+ <li>
+ poisci pod # SYSTEM INFORMATION
+ </li>
+ </ol>
+ </li>
+ <li>
+ Poskrbi, da bo SNMP streznik prek SNMP pod NET-SNMP-EXTEND-MIB::nsExtendOutpucd
+ k t2Table sporocal, koliko casa je vklopljen v sekundah.
+ <ol type="disc">
+ <li>
+ ustvari skripto upseconds, v kateri je zapisano sledece:<br>
+ <i>#!/bin/bash<br>
+ uptime=$(&lt;/proc/uptime)<br>
+ seconds=${uptime%%.*}<br>
+ echo "Uptime in seconds:" $seconds<br>
+ exit 0</i><br>
+ skripto nato shrani kjerkoli hoces
+ </li>
+ <li>
+ ne pozabi skripti dodelti pravic, da jo lahko zaganjamo:<br>
+ ukaz <i>chmod +x /pot_do_skripte/upseconds</i>
+ </li>
+ <li>
+ Nato uredi datoteko <i>snmpd.conf</i> in pod # EXTENDING THE AGENT
+ zakomentiraj vse tri teste ter dodaj svojo skripto upseconds.
+ Zgledati bi moralo nekako tako:<br>
+ <i># extend test1 /bin/echo Hello, world!<br>
+ # extend-sh test2 echo Hello, world! ; echo Hi there ; exit 35<br>
+ # extend-sh test3 /bin/sh /tmp/shtest<br>
+ extend-sh "ime_testa" "pot_to_skripte_upseconds"
+ </i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ Potrebno je ponovno zagnati snmp storitev.
+ <ol type="disc">
+ <li>
+ ukaz <i>/etc/init.d/snmpd restart</i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ Lahko testiras svoje nastavitve preko localhost-a.
+ <ol type="disc">
+ <li>
+ ukaz <i>snmpwalk localhost -c public -v1</i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ Stestiraj tudi, ali SNMP vrne pravilni cas delovanja (uptime) v sekundah
+ pod NET-SNMP-EXTEND-MIB::nsExtendOutput2Table.
+ <ol type="disc">
+ <li>
+ ukaz <i>snmpwalk -c students -v1 IPnaslovStreznika
+ 1.3.6.1.4.1.8072.1.3.2.4.1.2</i>
+ </li>
+ <li>
+ moral bi vrniti taksen string: "Uptime in seconds: xyz"
+ </li>
+ </ol>
+ </li>
+ </ol>
+
+ <h3>Nastavitev SNMPClient virtualke</h3>
+ <ol>
+ <li>
+ Nasnemi snmpd and snmp paketa.
+ <ol type="disc">
+ <li>
+ ukaz <i>apt-get install snmpd snmp</i>
+ </li>
+ </ol>
+ <li>
+ PRIPOROCILO! Preden spreminjate vaso datoteko /etc/snmp/snmpd.conf,
+ naredite kopijo originalne datoteke.
+ <ol type="disc">
+ <li>
+ ukaz <i>cp /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf.orig</i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ Nastavi snmp streznik tako, da se bodo lahko nanj povezali drugi racunalniki
+ (popravi datoteko snmpd.conf).
+ <ol type="disc">
+ <li>
+ ukaz <i>nano /etc/snmp/snmpd.conf</i>
+ (lahko uporabis drug urejevalnik besedila)
+ </li>
+ <li>
+ odkomentiraj sledece stiri vrstice:<br>
+ <i># Listen for connections from the local system only<br>
+ # agentAddress udp:127.0.0.1:161<br>
+ # Listen for connections on all interfaces (both IPv4 *and* IPv6)<br>
+ agentAddress udp:161,udp6:[::1]:161</i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ Dodaj skupino (community) racunalnikov, ki lahko dostopajo do vseh podatkov.
+ To skupino bomo poimenovali students (spet potrebno spremeniti datoteko)
+ snmpd.conf file.
+ <ol type="disc">
+ <li>
+ rocommunity students 0.0.0.0/0
+ </li>
+ <li>
+ napisi pravilni naslov omrezja = ukaz <i>ifconfig -a</i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ DODATNA (NEOBVEZNA) KONFIGURACIJA. Nizje v isti datoteki lahko nastavis
+ lokacijo racunalnika, na katerem deluje snmp, ter ime administratorja.
+ <ol type="disc">
+ <li>
+ poisci pod # SYSTEM INFORMATION
+ </li>
+ </ol>
+ </li>
+
+ <li>
+ Potrebno je ponovno zagnati snmp storitev.
+ <ol type="disc">
+ <li>
+ ukaz <i>/etc/init.d/snmpd restart</i>
+ </li>
+ </ol>
+ </li>
+ <li>
+ Lahko testiras svoje nastavitve preko localhost-a.
+ <ol type="disc">
+ <li>
+ ukaz <i>snmpwalk localhost -c public -v1</i>
+ </li>
+ </ol>
+ </li>
+ <li>Ustvari uporabnika <i>test</i> z geslom <i>test</i>
+ <ol type="disc">
+ <li>
+ ukaz <i>adduser test</i>
+ </li>
+ </ol>
+ </li>
+ <li>Prijavi se kot uporabnik <i>test</i> ter naredi skripto <i>upminutes</i>
+ v domacem direktoriju. Ta skripta naj izpisuje cas delovanja racunalnika (uptime) v minutah.
+ <ol type="disc">
+ <li>
+ ukaz <i>nano upminutes</i> (lahko uporabis drug urejevalnik besedila)
+ </li>
+ <li>
+ dodaj sledeco kodo:<br>
+ <i>#!/bin/bash<br>
+ uptime=$(&lt;/proc/uptime)<br>
+ uptime=${uptime%%.*}<br>
+ minutes=$(( uptime / 60 ))<br>
+ echo $minutes<br>
+ exit 0</i>
+ </li>
+ <li>
+ ne pozabi skripti dodelti pravic, da jo lahko zaganjamo:<br>
+ ukaz <i>chmod +x /pot_do_skripte/upminutes</i>
+ </li>
+ <li>
+ stestiraj skripto (izpisovati bi morala cas delovanja racunalnika (uptime) v minutah)<br>
+ ukaz <i>/home/test/upminutes</i>
+ </li>
+ </ol>
+ </li>
+ <li>Kot uporabnik <i>test</i> naredi se eno skripto <i>beri.sh</i>m ki bere
+ vrednosti od OID 1.3.6.1.4.1.8072.1.3.2.4.1.2.
+ na SNMPServer. Shrani jo na SNMPClient v domac direktorij uporabnika test.
+ <ol type="disc">
+ <li>
+ ukaz <i>nano beri.sh</i> (lahko uporabis drug urejevalnik besedila)
+ </li>
+ <li>
+ dodaj kodo:<br>
+ <i>#!/bin/bash<br>
+ snmpwalk -c students -v1 IPnaslovStreznika 1.3.6.1.4.1.8072.1.3.2.4.1.2<br>
+ exit 0</i>
+ </li>
+ <li>
+ ne pozabi skripti dodelti pravic, da jo lahko zaganjamo:<br>
+ ukaz <i>chmod +x /pot_do_skripte/upminutes</i>
+ </li>
+ </ol>
+ </li>
+ </ol>
+
+ </body>
+</html>
diff --git a/tasks/snmp_agent_uptime/task.py b/tasks/snmp_agent_uptime/task.py
new file mode 100644
index 0000000..919fcb4
--- /dev/null
+++ b/tasks/snmp_agent_uptime/task.py
@@ -0,0 +1,224 @@
+# kpov_util should be imported by add_assignment.py
+
+# TODO: finish this.
+instructions = {
+ 'si': '''\
+<p>
+Postavi tri navidezne računalnike: <em>SimpleArbiter</em>, <em>SNMPServer</em> in <em>SNMPClient</em>.
+
+<p>
+Napiši program <code>upminutes</code>, ki bo izpisal v minutah, koliko časa je racunalnik vklopljen. Postavi ga na <em>SNMPClient</em> v domači imenik uporabnika <code>test</code> z geslom <code>test</code>.
+
+<p>
+Poskrbi, da bo strežnik SNMP pod OID
+
+<pre><code>{{SNMP_UPTIME_OID}}</code></pre>
+
+<p>
+sporočal, koliko časa je vklopljen v sekundah.
+
+<p>
+Napiši skripto, poimenovano <code>beri.sh</code>, ki prek SNMP prebere vrednost s <em>simpleArbiterDhcpGWSNMP</em> na OID
+
+<pre><code>{{SNMP_CLIENT_OID}}</code></pre>
+
+<p>
+kot član skupnosti <code>testers</code>. Postavi jo na <em>SNMPClient</em>, v domači imenik uporabnika <code>test</code>. Poskrbi, da bodo podatki na SNMPServer dostopni za skupino (angl. <em lang="en">community</em>) <code>studentje</code>.
+''',
+ 'en': '''\
+<p>
+Set up three virtual computers: <em>SimpleArbiter</em>, <em>SNMPServer</em> and <em>SNMPClient</em>.
+
+<p>
+Write a program called <code>upminutes</code>. This program should output the uptime of the computer in minutes. Set it up on <em>SNMPClient</em> in the home directory of the user <code>test</code> with the password <code>test</code>.
+
+<p>
+Make sure that the SNMP server reports its uptime in seconds over SNMP under OID
+
+<pre><code>{{SNMP_UPTIME_OID}}</code></pre>
+
+<p>
+Write a script called <code>beri.sh</code> that reads the value from the OID
+
+<pre><code>{{SNMP_CLIENT_OID}}</code></pre>
+
+<p>
+on <em>simpleArbiterDhcpGWSNMP</em> as a member of the community <code>testers</code>. Set it up on <em>SNMPClient</em> in the home directory of the user <code>test</code>. Make all the data available over SNMP readable by the community <code>studentje</code>.
+''',
+}
+
+computers = {
+ 'SNMPClient': {
+ 'disks': [
+ { 'name': 'student-SNMPClient',
+
+ },
+ ],
+ 'network_interfaces': [{'network': 'net1'}],
+ 'flavor': 'm1.tiny',
+ 'config_drive': False
+ },
+ 'SNMPServer': {
+ 'disks': [
+ { 'name': 'student-SNMPServer',
+ },
+ ],
+ 'network_interfaces': [{'network': 'net1'}],
+ 'flavor': 'm1.tiny',
+ 'config_drive': False
+
+ },
+
+ 'SimpleArbiter': {
+ 'disks': [
+ { 'name': 'simpleArbiterDhcpGWSNMP',
+ },
+ ],
+ 'network_interfaces': [{'network': 'net1'}, {'network': 'test-net'}],
+ 'flavor': 'm1.tiny',
+ 'config_drive': False
+ }
+}
+
+networks = { 'net1': {'public': False}, 'test-net': {'public': True} }
+
+params_meta = {
+ 'SNMP_VALUE': {'descriptions': {'si': 'Vrednost, dostopna prek SNMP', 'en': 'The value available over SNMP'}, 'w': False, 'public':False, 'type': 'short', 'generated': True},
+ 'SNMP_UPTIME_OID': {'descriptions': {'si': 'SNMP_UPTIME_OID (za uptime)', 'en': 'SNMP_UPTIME_OID (for the uptime)'}, 'w': False, 'public':True, 'type': 'str', 'generated': True},
+ 'SNMP_CLIENT_OID': {'descriptions': {'si': 'SNMP_CLIENT_OID, ki naj ga klient bere', 'en':'The OID that the client should read'}, 'w': False, 'public':True, 'type': 'OID', 'generated': True},
+ 'SERVER_IP': {'descriptions': {'si': 'IP SNMP strežnika', 'en':'IP of the SNMP server'}, 'w': True, 'public':True, 'type': 'IP', 'generated': False},
+ 'CLIENT_IP': {'descriptions': {'si': 'IP SNMP klienta', 'en': 'IP of the SNMP client'}, 'w': True, 'public':True, 'type': 'IP', 'generated': False},
+
+}
+
+def task(SERVER_IP, CLIENT_IP, SNMP_UPTIME_OID, SNMP_CLIENT_OID):
+ #<== Aleksander Fujs 6310020 ==>
+ # TODO popravi IPje
+ import netsnmp
+ import paramiko
+ from paramiko import SSHClient
+ return_results = {}
+
+ client = SSHClient()
+ client.load_system_host_keys()
+ client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
+
+ client.connect(SERVER_IP, username='student', password='vaje')
+ stdin, stdout, stderr = client.exec_command('uptime=$(</proc/uptime); uptime=${uptime%%.*}; echo $uptime')
+ return_results['server_uptime'] = stdout.read()
+
+ try:
+ session = netsnmp.Session(DestHost=SERVER_IP, Version=2, Community='studentje')
+ # results_objs = netsnmp.VarList(netsnmp.Varbind(SNMP_UPTIME_OID)) #117.112.116.105.109.101 <-uptime
+ results_objs = netsnmp.VarList(netsnmp.Varbind('.1.3.6.1.4.1.8072.1.3.2.4.1.2')) #117.112.116.105.109.101 <-uptime
+ session.walk(results_objs)
+ result_list = []
+ for result in results_objs:
+ result_list.append('{}.{}: {}'.format(
+ result.tag, result.iid, result.val))
+ return_results['server_OID'] = "\n".join(result_list)
+ except Exception as exception_error:
+ # Check for errors and print out results
+ print(('ERROR: Occurred during SNMPget for OID %s from %s: '
+ '(%s)') % (SNMP_UPTIME_OID, CLIENT_IP, exception_error))
+ sys.exit(2)
+
+ client.connect(CLIENT_IP, username='test', password='test')
+ stdin, stdout, stderr = client.exec_command('uptime=$(</proc/uptime); uptime=${uptime%%.*}; echo $(( uptime ))')
+ return_results['client_uptime'] = stdout.read()
+ # zakaj bi morala biti ravno bash skripta?
+ stdin, stdout, stderr = client.exec_command('/home/test/upminutes')
+ #TODO preverit da ni v skripti hardcodan
+ return_results['client_script'] = stdout.read()
+
+ stdin, stdout, stderr = client.exec_command('/home/test/beri.sh')
+ return_results['client_script2'] = stdout.read()
+ #TODO add 3 part of assigement
+ return return_results
+ #<== Aleksander Fujs 6310020 ==>
+
+
+def gen_params(user_id, params_meta):
+ import random
+ params = dict()
+ r = random.Random(user_id)
+ # You can also create an OID creation function in kpov_util.
+ # this should probably return params_meta
+
+ #<== Aleksander Fujs 6310020 ==>
+ params['SNMP_VALUE'] = kpov_util.alnum_gen(r, 64)
+ params['SNMP_UPTIME_OID'] = 'NET-SNMP-EXTEND-MIB::nsExtendOutLine."{}".1'.format(
+ kpov_util.hostname_gen(r))
+ params['SNMP_CLIENT_OID'] = '1.3.6.1.4.1.8072.2.9999.9999.{}'.format(
+ r.randint(0, 255))
+ #<== Aleksander Fujs 6310020 ==>
+
+ return params
+
+def task_check(results, params):
+ #TODO improve regex
+ import re
+ score = 0
+ hints = []
+ client_script_uptime = int(results['client_script'].strip())
+ client_uptime = int(results['client_uptime'].strip())
+ d = client_uptime - client_script_uptime*60
+ if d >= 0 and d < 62:
+ score += 3
+ else:
+ hints += ["client uptime script output wrong."]
+ server_uptime = int(results['server_uptime'].strip())
+ lines = results['server_OID'].split('\n')
+ unique_part_start = params['SNMP_UPTIME_OID'].find('"')
+ unique_part_end = params['SNMP_UPTIME_OID'].find('"', unique_part_start+1)
+ unique_part = params['SNMP_UPTIME_OID'][unique_part_start+1:unique_part_end]
+ server_oid = 'iso.3.6.1.4.1.8072.1.3.2.4.1.2.{}.'.format(len(unique_part))
+ server_oid += '.'.join([str(ord(i)) for i in unique_part]) + '.1.'
+ found_uptime = False
+ for line in lines:
+ try:
+ oid, uptime_s = line.split(':')
+ d = server_uptime - int(uptime_s.strip())
+ if oid.strip() == server_oid and d >= -2 and d <= 5:
+ found_uptime = True
+ break
+ except Exception as e:
+ pass
+ if len(lines) <= 50 and found_uptime:
+ score += 3
+ else:
+ hints += ["Uptime not found in server's MDB"]
+ if results['client_script2'].find(params['SNMP_VALUE']) >= 0:
+ score += 4
+ else:
+ hints += ["beri.sh not working properly"]
+ return score, hints
+
+def prepare_disks(templates, task_params, global_params):
+# d = templates['simpleArbiterDhcp']
+ prog = """#!/usr/bin/python
+import sys
+action = sys.argv[1]
+oid = sys.argv[2]
+
+foo_oid = ".{oid}"
+foo_oid_start = foo_oid[:foo_oid.rfind('.')]
+foo_oid_end = foo_oid[len(foo_oid_start)+1:]
+oid_end = oid[len(foo_oid_start)+1:]
+oid_start = oid[:len(foo_oid_start)]
+
+if action == '-n' and (
+ (oid_start == foo_oid_start) and (
+ (len(oid_end) == 0) or (int(oid_end) < int(foo_oid_end))
+ )
+ ):
+ oid = foo_oid
+
+if action != '-s' and oid == foo_oid:
+ print foo_oid
+ print "string"
+ print "{val}"
+""".format(oid = task_params['SNMP_CLIENT_OID'], val = task_params['SNMP_VALUE'])
+ templates['simpleArbiterDhcpGWSNMP'].write('/usr/local/bin/snmpext.py', prog)
+ templates['simpleArbiterDhcpGWSNMP'].chmod(0o755, '/usr/local/bin/snmpext.py')
+ write_default_config(templates['simpleArbiterDhcpGWSNMP'], global_params)