#include "dogcore.h"
#include "boxshm.h"

DogCore::DogCore(QObject *parent) : QObject(parent)
{
    logThread = new LogThread(this);
    logThread->start();
    isWorking = false;

    timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(time_out()));
    for(int i=0;i<16;i++)
        chkTime[i] = QDateTime::currentDateTime().toTime_t();
}

unsigned int DogCore::chkrootprocmem(QString proname)
{
    FILE *f1=NULL,*f2=NULL;
    int pid, vmrss;
    char buff[512],name[64];
    QString cmd = QString("pgrep %1 -u root").arg(proname);
    f1 = popen(cmd.toUtf8().data(),"r");
    if(f1==NULL)
        return 0;
    if(NULL == fgets(buff,512,f1)){
        pclose(f1);
        return 0;
    }
    pid = atoi(buff);
    pclose(f1);
    QString filename = QString("/proc/%1/status").arg(pid);
    f2 = fopen(filename.toUtf8().data(),"r");
    if(NULL == f2)
        return 0;
    for(int i=0;i<16;i++)
        fgets(buff,512,f2);
    fgets(buff,512,f2);
    sscanf(buff,"%s %d",name,&vmrss);
    fclose(f2);
    return (vmrss>>10);
}

void DogCore::time_out()
{
    if(!isWorking){
        isWorking = true;
        uint chkTime1 = QDateTime::currentDateTime().toTime_t();
        if((chkTime1-chkTime[1])>=300){
            chkTime[1]=chkTime1;
            if(chkrootprocmem("agDP0001")>200){
                logThread->appendData(QString("[%1 agBoxDog %2] %3 chkrootprocmem(agDP0001): %4")
                                      .arg(QDateTime::fromTime_t(chkTime1).toString("yyyy-MM-dd HH:mm:ss"))
                                      .arg(chkTime1)
                                      .arg(chkTime[1])
                                      .arg(chkrootprocmem("agDP0001")));
                system("killall agDP0001");
                sleep(1);
                system("/root/bin/agDP0001 &");
            }

        }
        if((agBoxShm->processStatus[1].t_time>0)
                &&(static_cast<int>(chkTime1-agBoxShm->processStatus[1].t_time)>60)){
            if(system("killall agDP0001")!=-1){
                if(system("/root/bin/agDP0001 &")!=-1){
                    logThread->appendData(QString("[ %1 agBoxDog %2 ] agDP0001 time out: %3")
                                          .arg(QDateTime::fromTime_t(chkTime1).toString("yyyy-MM-dd HH:mm:ss"))
                                          .arg(chkTime1)
                                          .arg(static_cast<int>(chkTime1-agBoxShm->processStatus[1].t_time)));
                }
            }
        }

        if((chkTime1-chkTime[2])>300){
            chkTime[2]=chkTime1;
            if(chkrootprocmem("agDP0002")>200){
                logThread->appendData(QString("[%1 agBoxDog %2] %3 chkrootprocmem(agDP0002): %4")
                                      .arg(QDateTime::fromTime_t(chkTime1).toString("yyyy-MM-dd HH:mm:ss"))
                                      .arg(chkTime1)
                                      .arg(chkTime[2])
                                      .arg(chkrootprocmem("agDP0002")));
                system("killall agDP0002");
                sleep(1);
                system("/root/bin/agDP0002 &");
            }

        }
        if((agBoxShm->processStatus[2].t_time>0)
                &&(static_cast<int>(chkTime1-agBoxShm->processStatus[2].t_time)>60)){
            if(system("killall agDP0002")!=-1){
                if(system("/root/bin/agDP0002 &")!=-1){
                    logThread->appendData(QString("[%1 agBoxDog %2] agDP0002 time out: %3")
                                          .arg(QDateTime::fromTime_t(chkTime1).toString("yyyy-MM-dd HH:mm:ss"))
                                          .arg(chkTime1)
                                          .arg(static_cast<int>(chkTime1-agBoxShm->processStatus[2].t_time)));
                }

            }

        }

        if((chkTime1-chkTime[3])>300){
            chkTime[3]=chkTime1;
            if(chkrootprocmem("agDP0003")>200){
                logThread->appendData(QString("[%1 agBoxDog %2] %3 chkrootprocmem(agDP0003): %4")
                                      .arg(QDateTime::fromTime_t(chkTime1).toString("yyyy-MM-dd HH:mm:ss"))
                                      .arg(chkTime1)
                                      .arg(chkTime[3])
                                      .arg(chkrootprocmem("agDP0003")));
                system("killall agDP0003");
                sleep(1);
                system("/root/bin/agDP0003 &");
            }
        }
        if((agBoxShm->processStatus[3].t_time>0)
                &&(static_cast<int>(chkTime1-agBoxShm->processStatus[3].t_time)>60)){
            if(system("killall agDP0003")!=-1){
                if(system("/root/bin/agDP0003 &")!=-1){
                    logThread->appendData(QString("[%1 agBoxDog %2] agDP0003 time out: %3")
                                          .arg(QDateTime::fromTime_t(chkTime1).toString("yyyy-MM-dd HH:mm:ss"))
                                          .arg(chkTime1)
                                          .arg(static_cast<int>(chkTime1-agBoxShm->processStatus[3].t_time)));
                }
            }
        }

        if((chkTime1-chkTime[4])>300){
            chkTime[4]=chkTime1;
            if(chkrootprocmem("agDP0004")>200){
                logThread->appendData(QString("[%1 agBoxDog %2] %3 chkrootprocmem(agDP0004): %4")
                                      .arg(QDateTime::fromTime_t(chkTime1).toString("yyyy-MM-dd HH:mm:ss"))
                                      .arg(chkTime1)
                                      .arg(chkTime[4])
                                      .arg(chkrootprocmem("agDP0004")));
                system("killall agDP0004");
                sleep(1);
                system("/root/bin/agDP0004 &");
            }
        }
        if((agBoxShm->processStatus[4].t_time>0)
                &&(static_cast<int>(chkTime1-agBoxShm->processStatus[4].t_time)>60)){
            if(system("killall agDP0004")!=-1){
                if(system("/root/bin/agDP0004 &")!=-1){
                    logThread->appendData(QString("[%1 agBoxDog %2] agDP0004 time out: %3")
                                          .arg(QDateTime::fromTime_t(chkTime1).toString("yyyy-MM-dd HH:mm:ss"))
                                          .arg(chkTime1)
                                          .arg(static_cast<int>(chkTime1-agBoxShm->processStatus[4].t_time)));
                }
            }
        }

        if((chkTime1-chkTime[5])>300){
            chkTime[5]=chkTime1;
            if(chkrootprocmem("agDP0005")>200){
                logThread->appendData(QString("[%1 agBoxDog %2] %3 chkrootprocmem(agDP0005): %4")
                                      .arg(QDateTime::fromTime_t(chkTime1).toString("yyyy-MM-dd HH:mm:ss"))
                                      .arg(chkTime1)
                                      .arg(chkTime[5])
                                      .arg(chkrootprocmem("agDP0005")));
                system("killall agDP0005");
                sleep(1);
                system("/root/bin/agDP0005 &");
            }
        }
        if((agBoxShm->processStatus[5].t_time>0)
                &&(static_cast<int>(chkTime1-agBoxShm->processStatus[5].t_time)>60)){
            if(system("killall agDP0005")!=-1){
                if(system("/root/bin/agDP0005 &")!=-1){
                    logThread->appendData(QString("[%1 agBoxDog %2] agDP0005 time out: %3")
                                          .arg(QDateTime::fromTime_t(chkTime1).toString("yyyy-MM-dd HH:mm:ss"))
                                          .arg(chkTime1)
                                          .arg(static_cast<int>(chkTime1-agBoxShm->processStatus[5].t_time)));
                }
            }
        }

        if((chkTime1-chkTime[6])>300){
            chkTime[6]=chkTime1;
            if(chkrootprocmem("agDP0006")>200){
                logThread->appendData(QString("[%1 agBoxDog %2] %3 chkrootprocmem(agDP0006): %4")
                                      .arg(QDateTime::fromTime_t(chkTime1).toString("yyyy-MM-dd HH:mm:ss"))
                                      .arg(chkTime1)
                                      .arg(chkTime[6])
                                      .arg(chkrootprocmem("agDP0006")));
                system("killall agDP0006");
                sleep(1);
                system("/root/bin/agDP0006 &");
            }
        }
        if((agBoxShm->processStatus[6].t_time>0)
                &&(static_cast<int>(chkTime1-agBoxShm->processStatus[6].t_time)>90)){
            if(system("killall agDP0006")!=-1){
                if(system("/root/bin/agDP0006 &")!=-1){
                    logThread->appendData(QString("[%1 agBoxDog %2] agDP0006 time out: %3")
                                          .arg(QDateTime::fromTime_t(chkTime1).toString("yyyy-MM-dd HH:mm:ss"))
                                          .arg(chkTime1)
                                          .arg(static_cast<int>(chkTime1-agBoxShm->processStatus[6].t_time)));
                }
            }
        }


        isWorking = false;
    }

}


void DogCore::start(){
    time_out();
    timer->start(15000);
}











