import whatap.pack.tagcount_pack as tagcount_pack
import whatap.osinfo as osinfo
import whatap.agent.secure.security_master as secure


class TaskInfraNetstat:
    NETSTAT = "infra_netstat"

    def __init__(self, interval=60):
        self.intervalValue = interval
        self.recType = 1

    def interval(self):
        return self.intervalValue

    def process(self, os_, pcode, oid, now):
        counters = osinfo.GetNetstatCounters()
        if not counters:
            return None

        secu = secure.GetSecurityMaster()
        p = tagcount_pack.TagCountPack()
        p.pcode = pcode
        p.oid = oid
        p.time = now
        p.category = TaskInfraNetstat.NETSTAT
        p.putTag("oname", str(secu.oname))
        p.putTag("!rectype", str(self.recType))

        p.put("netstat_Unknown", counters.get("netstat_Unknown", 0))
        p.put("netstat_Established", counters.get("netstat_Established", 0))
        p.put("netstat_Syn_Sent", counters.get("netstat_Syn_Sent", 0))
        p.put("netstat_Syn_Recv", counters.get("netstat_Syn_Recv", 0))
        p.put("netstat_Fin_Wait1", counters.get("netstat_Fin_Wait1", 0))
        p.put("netstat_Fin_Wait2", counters.get("netstat_Fin_Wait2", 0))
        p.put("netstat_Time_Wait", counters.get("netstat_Time_Wait", 0))
        p.put("netstat_Close_Wait", counters.get("netstat_Close_Wait", 0))
        p.put("netstat_Last_Ack", counters.get("netstat_Last_Ack", 0))
        p.put("netstat_Listen", counters.get("netstat_Listen", 0))
        p.put("netstat_Closing", counters.get("netstat_Closing", 0))
        p.put("Server_Filedescriptors", counters.get("Server_Filedescriptors", 0))

        return p
