# SPDX-License-Identifier: AGPL-3.0-or-later # kpov_util should be imported by add_assignment.py # TODO: finish this. instructions = { 'si': '''\

Postavi tri navidezne računalnike: SimpleArbiter, SNMPServer in SNMPClient.

Napiši program upminutes, ki bo izpisal v minutah, koliko časa je racunalnik vklopljen. Postavi ga na SNMPClient v domači imenik uporabnika test z geslom test.

Poskrbi, da bo strežnik SNMP pod OID

{{SNMP_UPTIME_OID}}

sporočal, koliko časa je vklopljen v sekundah.

Napiši skripto, poimenovano beri.sh, ki prek SNMP prebere vrednost s simpleArbiterDhcpGWSNMP na OID

{{SNMP_CLIENT_OID}}

kot član skupnosti testers. Postavi jo na SNMPClient, v domači imenik uporabnika test. Poskrbi, da bodo podatki na SNMPServer dostopni za skupino (angl. community) studentje. ''', 'en': '''\

Set up three virtual computers: SimpleArbiter, 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 its 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 simpleArbiterDhcpGWSNMP as a member of the community testers. Set it up on SNMPClient in the home directory of the user test. Make all the data available over SNMP readable by the community studentje. ''', } 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=$( 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)