#!/usr/bin/env python

"""
Assignment 3-2:
Use SNMPv3 to create two SVG image files.

Image1: input/output bytes on pynet-rtr1-Fa4
Image2: input/output packets on pynet-rtr1-Fa4

Get bytes and packets every five minutes for an hour, compute the differences and graph each point over time

Relevant OIDs:

('ifDescr_fa4', '1.3.6.1.2.1.2.2.1.2.5')
('ifInOctets_fa4', '1.3.6.1.2.1.2.2.1.10.5')
('ifInUcastPkts_fa4', '1.3.6.1.2.1.2.2.1.11.5')
('ifOutOctets_fa4', '1.3.6.1.2.1.2.2.1.16.5'),
('ifOutUcastPkts_fa4', '1.3.6.1.2.1.2.2.1.17.5')


Command line syntax:
]$ SNMP_Graphing <ip_addr> <username> <graph_file_name> <graphing_interval_minutes> <num_iterations> <ifDescr_OID>...
<OID1> [<OID2> [<OID3> [<OID4>]]]
<will prompt for SNMPv3 auth_key/encrypt_key>

Plan:
Create my_router object, get SNMP key, download data. If interval is specified, sleep for interval, do until
num_iterations. If interval is not specified, run once and terminate. Either way, store diffs in a data structure in
a pickle file.


"""

import pygal
import time
import sys
import pickle
import getpass
import os.path
import json
import snmp_helper


from collections import namedtuple

DEBUG = False
SNMP_PORT = 161
PICKLE_FILE = "SNMP_Graphing_pkl.p"
SYSNAME = "SysName"
DESCRIPTION = "ifDescr"


class RouterOID(object):
    """
    Represents a router OID, the keys to measure it, a list of time deltas that measurements were taken,
    and a list of the measurements taken at those times
    """
    def __init__(self, ip_addr, username, auth_encr_key, oid_name, oid):
        self.router = (ip_addr, SNMP_PORT)
        self.snmp_user = (username, auth_encr_key, auth_encr_key)
        self.oid_name = oid_name
        self.oid = oid
        self.timestamp = 0
        self.total_measurement = 0
        self.num_data_points = 0
        self.time_diff_list = []
        self.measurement_diff_list = []

    def add_data_point(self):
        timestamp = int(time.time())
        if self.timestamp:
            time_diff = timestamp - self.timestamp
        else:
            time_diff = 0
        self.timestamp = timestamp
        self.time_diff_list.append(time_diff)