#!/usr/bin/env python3
"""
Kuhn & Rueß GmbH
Consulting and Development
https://kuhn-ruess.de
"""
from sys import argv
import pyodbc


class AgentAs400():
    """
    Agent AS400
    """

    def __init__(self):
        """
        Init
        """
        driver = argv[1]
        system = argv[2]
        uid = argv[3]
        pwd = argv[4]

        self.connection = pyodbc.connect(
           driver=driver,
           system=system,
           uid=uid,
           pwd=pwd)

    def get_jobs(self):
        """
        Return List of Jobs
        """
        query = 'SELECT * FROM TABLE(QSYS2.ACTIVE_JOB_INFO())'
        c1 = self.connection.cursor()
        c1.execute(query)

        print("<<<as400_agent_jobs>>>")

        field_names = [description[0] for description in c1.description]
        for row in c1:
            job_dict = dict(zip(field_names, row))
            print(f"[[[{job_dict['JOB_NAME']}]]]")
            for key, value in job_dict.items():
                print(f"{key} {value}")

    def get_asp(self):
        """
        Return ASP usage from QSYS2.ASP_INFO.
        Capacities are reported in megabytes (iSeries convention, 10^6 bytes).
        """
        query = (
            "SELECT ASP_NUMBER, ASP_TYPE, ASP_STATE, "
            "TOTAL_CAPACITY, TOTAL_CAPACITY_AVAILABLE, "
            "OVERCOMMIT_STORAGE, STORAGE_THRESHOLD_PERCENTAGE "
            "FROM QSYS2.ASP_INFO"
        )
        c1 = self.connection.cursor()
        c1.execute(query)

        print("<<<as400_agent_asp>>>")

        field_names = [description[0] for description in c1.description]
        for row in c1:
            asp_dict = dict(zip(field_names, row))
            print(f"[[[{asp_dict['ASP_NUMBER']}]]]")
            for key, value in asp_dict.items():
                print(f"{key} {value}")


if __name__ == "__main__":
    agent = AgentAs400()
    agent.get_jobs()
    agent.get_asp()
