#!/usr/bin/python3
import subprocess
from time import sleep

class Health:
    def __init__(self):
        self.app()

    def comando(self,orden):
        orden_s=orden.split(' ')
        res=subprocess.check_output(orden_s)
        #if res.decode()!='\n' or res.decode()!='':
        #    print(res.decode())
        return res.decode()

    def reemplazar(self,ruta_archivo, texto_out, texto_in):
        with open(ruta_archivo,'r') as archivo:
            contenido=archivo.read()
        contenido_2 = contenido.replace(texto_out,texto_in)

        with open(ruta_archivo, 'w') as archivo_nuevo:
            archivo_nuevo.write(contenido_2)

    def tipo(self):
        global nombre
        res=subprocess.check_output('machinename')
        nombre=res.decode().split('-')
        return nombre[0].lower() # return en minuscula
    
    def id(self):
        res=subprocess.check_output('machineid').decode()
        return int(res)
    
    def revisar_id(self,servicio):
        id = self.id()
        valor = self.comando('cat /etc/systemd/system/' + servicio + '.service')
        if servicio=='mining-cam':
            id_v = str(40000+id)
            if id_v not in valor:
                num = valor.find('mine-360.com:')
                id_c = valor[num+13:num+18]
                self.reemplazar('/etc/systemd/system/'+servicio+'.service',id_c,id_v)
                self.comando('systemctl daemon-reload')
                self.comando('systemctl restart '+servicio)
        elif servicio=='mining-web':
            id_v = str(20000+id)
            if id_v not in valor:
                num = valor.find('mine-360.com:')
                id_c = valor[num+13:num+18]
                self.reemplazar('/etc/systemd/system/'+servicio+'.service',id_c,id_v)
                self.comando('systemctl daemon-reload')
                self.comando('systemctl restart '+servicio)
        elif servicio=='mining-tunel':
            id_v = str(10000+id)
            if id_v not in valor:
                num = valor.find('-R :')
                id_c = valor[num+4:num+9]
                self.reemplazar('/etc/systemd/system/'+servicio+'.service',id_c,id_v)
                self.comando('systemctl daemon-reload')
                self.comando('systemctl restart '+servicio)

    def fix_mmr(self):
        with open('/etc/systemd/system/mining-web.service','r') as archivo:
            contenido=archivo.read()
        if '192.168.1.120' in contenido:
            self.reemplazar('/etc/systemd/system/mining-web.service','192.168.1.120','localhost')
            self.comando('sudo systemctl daemon-reload')
            self.comando('sudo systemctl restart mining-web')
        
    
    def app(self):
        while True:
            try:
                if self.tipo()=='cam' or self.tipo()=='vis':
                    try:
                        self.revisar_id('mining-web')
                        st_web=self.comando('systemctl status mining-web')    
                        if 'running' not in st_web or 'dead' in st_web:
                            r1=self.comando('systemctl restart mining-web')
                        if 'daemon-reload' in st_web:
                            self.comando('sudo systemctl daemon-reload')
                            self.comando('systemctl restart mining-web')
                    except:
                        try:
                            self.comando('systemctl enable mining-web.service')
                            self.comando('systemctl restart mining-web')
                            ###ACA AGREGAR LO DE AGREGAR EL SERVICIO
                        except: pass

                    try:
                        self.revisar_id('mining-cam')    
                        st_cam=self.comando('systemctl status mining-cam')
                        if 'running' not in st_cam or 'dead' in st_cam:
                            r2=self.comando('systemctl restart mining-cam')
                        if 'daemon-reload' in st_cam:
                            self.comando('sudo systemctl daemon-reload')
                            self.comando('systemctl restart mining-cam')
                    except:
                        try:
                            self.comando('systemctl enable mining-cam.service')
                            self.comando('systemctl restart mining-cam')
                             ###ACA AGREGAR LO DE AGREGAR EL SERVICIO
                        except: pass

                elif self.tipo()=='mmr' or self.tipo()=='tablet':
                    try:
                        self.revisar_id('mining-web')
                        self.fix_mmr()
                        st_web=self.comando('systemctl status mining-web')    
                        if 'running' not in st_web or 'dead' in st_web:
                            r1=self.comando('systemctl restart mining-web')
                        if 'daemon-reload' in st_web:
                            self.comando('sudo systemctl daemon-reload')
                            self.comando('systemctl restart mining-web')
                    except:
                        try:
                            self.comando('systemctl enable mining-web.service')
                            self.comando('systemctl restart mining-web')
                             ###ACA AGREGAR LO DE AGREGAR EL SERVICIO
                        except: pass

                try:
                    self.revisar_id('mining-tunel')
                    st_tunel=self.comando('systemctl status mining-tunel')
                    if 'running' not in st_tunel or 'dead' in st_tunel:
                            r3=self.comando('systemctl restart mining-tunel')
                    if 'daemon-reload' in st_tunel:
                            self.comando('sudo systemctl daemon-reload')
                            self.comando('systemctl restart mining-tunel')
                except:
                    try:
                        self.comando('systemctl enable mining-tunel.service')
                        self.comando('systemctl restart mining-tunel')
                    except: pass
                
            except:
                pass

            sleep(60)

sanidad=Health()



    