import os
def limpezaconf():
    local = os.path.dirname(__file__)
    print("Realizando limpeza")
    fonte = os.path.basename(__file__)
    os.system("rm -f {}/{}".format(local,fonte))
def configfile():
    print( "Gerando arquivo de configuracao...")
    if os.path.isfile("/etc/zabbix/zabbix_agent2.conf"):
        print("Gzipando /etc/zabbix/zabbix_agent2.conf")
        os.system("gzip /etc/zabbix/zabbix_agent2.conf")
    if os.path.isfile("/etc/zabbix/zabbix_agentd.conf"):
        zabbix1conf=open("/etc/zabbix/zabbix_agentd.conf","r")
        with zabbix1conf as file:
            for linha in file:
                if "Hostname=" in linha and "#" not in linha:
                    hostname = linha.split("=")[1]
                    print(hostname)
    else:
        hostname = input(str("Insira o nome do Agente: "))
    while len(hostname) < 3:
        hostname = input(str("Nome invalido! Por favor, insira o nome corretamente: "))
    arquivo = open("/etc/zabbix/zabbix_agent2.conf", "w")
    arquivo.write("""
# Zabbix Agente 2 | AMT
ServerActive=zabbix.amt.com.br
Hostname={}
HostMetadata=LINUX
AllowKey=system.run[*]
LogFileSize=10
Include=/etc/zabbix/zabbix_agent2.d/*.conf
PidFile=/var/run/zabbix/zabbix_agent2.pid
LogFile=/var/log/zabbix/zabbix_agent2.log

# Configuracoes relacionada aos plugins
PluginSocket=/run/zabbix/agent.plugin.sock
ControlSocket=/run/zabbix/agent.sock
Include=/etc/zabbix/zabbix_agent2.d/plugins.d/*.conf
                  
### Option: ForceActiveChecksOnStart
#	Perform active checks immediately after restart for first received configuration.
#	Also available as per plugin configuration, example: Plugins.Uptime.System.ForceActiveChecksOnStart=1
#
# Mandatory: no
# Range: 0-1
# Default:
# ForceActiveChecksOnStart=0""".format(hostname))
    print( "Reiniciando Zabbix Agent 2")
    os.system("systemctl enable zabbix-agent2")
    os.system("/etc/init.d/zabbix-agent2 restart")
    print("Removendo 200.189.96.27 no Zabbix Agent 1 se presente para evitar conflitos")
    os.system("sed -i 's/,200.189.96.27//g' /etc/zabbix/zabbix_agentd.conf")
    os.system("sed -i 's/,201.148.208.38//g' /etc/zabbix/zabbix_agentd.conf")
    os.system("sed -i 's/,zabbix.amt.com.br//g' /etc/zabbix/zabbix_agentd.conf")
    print( "Reiniciando Zabbix Agent")
    os.system("systemctl restart zabbix-agent")
    limpezaconf()
    if os.path.isfile("/etc/init.d/zabbix-agent"):
        os.system("/etc/init.d/zabbix-agent status")
        os.system("/etc/init.d/zabbix-agent2 status")
    elif not os.path.isfile("/etc/init.d/zabbix-agent"):
        os.system("systemctl status zabbix-agent")
        os.system("systemctl status zabbix-agent2")
    if not os.path.isfile("/usr/sbin/zabbix_agent2"):
        print("Zabbix Agent 2 nao encontrado!!! Erro na instalacao? Colocando os IPs do Zabbix 2 no Zabbix 1")
        os.system("sed -i 's/ServerActive=200.189.96.20/ServerActive=200.189.96.20,zabbix.amt.com.br/g' /etc/zabbix/zabbix_agentd.conf")
        os.system("systemctl restart zabbix-agent")
        if os.path.isfile("/etc/init.d/zabbix-agent"):
            os.system("/etc/init.d/zabbix-agent status")
        elif not os.path.isfile("/etc/init.d/zabbix-agent"):
            os.system("systemctl status zabbix-agent")
