#!/usr/bin/env python # -*- coding: utf-8 -*- # kpov_random_helpers should be imported by add_assignment.py # TODO: finish this. instructions = { 'si':u""" Naloga: Postavi tri navidezne racunalnike: - SimpleArbiter s sliko diska simpleArbiterDhcpGW, - SNMPServer in - SNMPClient. Napiši program upminutes, ki bo izpisal v minutah koliko casa je racunalnik vklopljen. Postavi ga na SNMPClient v domaci imenik uporabnika test z geslom test. Poskrbi, da bo SNMP strežnik prek SNMP pod OID {SNMP_UPTIME_OID} sporočal, koliko casa je vklopljen v sekundah. Napiši skripto, poimenovano beri.sh, ki prek SNMP prebere vrednost s strežnika SNMPServer na OID {SNMP_CLIENT_OID} Postavi jo na SNMP klienta, v domaci imenik uporabnika test z geslom test. Poskrbi, da bodo podatki na SNMPServer dostopni za skupino (community) studentje. """, 'en':u""" 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. Make sure that the SNMP server reports it's uptime in seconds over SNMP under OID {SNMP_UPTIME_OID}. Write a script called beri.sh that reads the value from the OID {SNMP_CLIENT_OID} on SNMPServer. Set it up on SNMPClient in the home directory of the user test. Make all the data available over SNMP readable by the "studentje" community. """ } computers = { 'SNMPClient': { 'disks': [ { 'name': 'student-SNMPClient', }, #{ 'name': 'CDROM', # 'options':{'readonly': True}, # 'parts': [],# no parts, no mounting. #} ], 'network_interfaces': [{'network': 'net1'}], 'flavor': 'm1.tiny', 'config_drive': False }, 'SNMPServer': { 'disks': [ { 'name': 'student-SNMPServer', }, #{ 'name': 'CDROM', # 'options':{'readonly': True}, # 'parts': [],# no parts, no mounting. #} ], 'network_interfaces': [{'network': 'net1'}], 'flavor': 'm1.tiny', 'config_drive': False }, 'SimpleArbiter': { 'disks': [ { 'name': 'simpleArbiterDhcpGW', # attempt automount }, #{ 'name': 'CDROM', # 'options': {'readonly': True}, # 'parts': [{'dev': 'b1', 'path': '/cdrom'}], #}, ], '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'}, 'w': False, 'public':False, 'type': 'short', 'generated': True}, 'SNMP_UPTIME_OID': {'descriptions': {'si': 'SNMP_UPTIME_OID (za uptime)'}, 'w': False, 'public':True, 'type': 'str', 'generated': True}, 'SNMP_CLIENT_OID': {'descriptions': {'si': 'SNMP_CLIENT_OID, ki naj ga klient bere'}, 'w': False, 'public':True, 'type': 'OID', 'generated': True}, 'SERVER_IP': {'descriptions': {'si': 'IP SNMP strežnika'}, 'w': True, 'public':True, 'type': 'IP', 'generated': False}, 'CLIENT_IP': {'descriptions': {'si': 'IP SNMP klienta'}, '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='root', password='kaboom') stdin, stdout, stderr = client.exec_command('uptime=$( 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_random_helpers. # this should probably return params_meta #<== Aleksander Fujs 6310020 ==> params['SNMP_VALUE'] = kpov_random_helpers.alnum_gen(r, 64) params['SNMP_UPTIME_OID'] = 'NET-SNMP-EXTEND-MIB::nsExtendOutLine."{}".1'.format( kpov_random_helpers.hostname_gen(r)) params['SNMP_CLIENT_OID'] = '1.3.6.1.4.1.8072.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 < 60: score += 3 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, e: pass if len(lines) <= 10 and found_uptime: score += 3 if results['client_script2'].find(params['SNMP_VALUE']) >= 0: score += 4 return score, hints def prepare_disks(templates, task_params, global_params): # d = templates['simpleArbiterDhcp'] prog = """#!/usr/bin/python import sys oid = sys.argv[2] action = sys.argv[1] if action == '-g' and oid == ".{oid}": print ".{oid}" print "string" print "{val}" elif action == '-n' and oid < ".{oid}": print ".{oid}" print "string" print "{val}" """.format(oid = task_params['SNMP_CLIENT_OID'], val = task_params['SNMP_VALUE']) templates['student-SNMPServer'].write('/usr/local/bin/snmpext.py', prog) templates['student-SNMPServer'].chmod(0766, '/usr/local/bin/snmpext.py') write_default_config(templates['simpleArbiterDhcpGW'], global_params)