Django environment to achieve real-time monitoring function

Results preview

 

 

 Server interface development method getEnvinfo

1. Installation dependencies (ssh Protocol Toolkit)

pip install paramiko

2. Host Configuration

host = {'ip': ip, 'port': port, 'username': username, 'password': password}

3. remote execution of commands and returns the results to obtain

Open # ssh client 
ssh = paramiko.SSHClient () 
# host is not set to accept list can be known_hosts ssh connection 
ssh.set_missing_host_key_policy (paramiko.AutoAddPolicy ()) 
ssh.connect (Host hostname = [ 'IP'], Port = Host [ 'Port'], Host username = [ 'username'], Host password = [ 'password']) 
# memory information acquired 
stdin, stdout, stderr = ssh.exec_command ( 'the sudo Free -m | grep Mem') 
str_out stdout.read = (). decode () 
totalmem = STR (str_out) .split ( "") [. 1] .replace ( "", "") 
FreeMem = STR (str_out) .split ( "") [2]. Replace ( "", "") 
ram_usage STR = (int (FreeMem) / int (totalmem) * 100) .split ( ".") [0] + "%" 
# cpu obtain information 
stdin, stdout, stderr = ssh. exec_command ( 'top -bn 1 -i -c|grep us|grep id')
str_out = str(stdout.read().decode()).replace(" ","")
STR = cpu_usage (100-int (str_out.split ( ",") [. 3] .split ( ".") [0])) + "%"
# Acquires disk information 
stdin, stdout, stderr = ssh.exec_command ( '-H DF | grep G | grep -o [0-9] *% | grep [0-9] [0-9]') 
str_out = stdout. Read (). decode () 
rom_usage = STR (str_out) .replace ( "\ n-", "") 
# ssh client Close 
ssh.close ()

4. The package of environmental information and returns

env_info={'host':host['ip'],'ramusage':ram_usage,'cpuusage':cpu_usage,'romusage':rom_usage}
return env_info

The front end of the dynamic Echo Request

$(function () {
    function getenvinfo(ip){
        if (ip.length>8){
            $.ajax({
                type:'GET',
                url:'http://localhost:8000/getEnvinfo',
                data:{
                    ip:ip
                },
                success:function (result) {
                    console.log(result)
                    $('#cpuusage').find("span")[0].textContent=result.cpuusage
                    $('#cpuusage').attr("style","width: "+result.cpuusage)
                    $('#romusage').find("span")[0].textContent=result.romusage
                    $('#romusage').attr("style","width: "+result.romusage)
                    $('#ramusage').find("span")[0].textContent=result.ramusage
                    $('#ramusage').attr("style","width: "+result.ramusage)
                },
                error:function (e) {
                    console.log(e.status);
                }
            });
        }
    }
    $('#ip').on('blur',function (){
        getenvinfo($(this).val())
    });
    $('#ip').keyup(function(event){
        if(event.keyCode ==13){
            getenvinfo($(this).val())
        }
    });
    setInterval(function (){
        getenvinfo($('#ip').h ()) 
}); 
    } 90 000)

Django-based framework to build

 

Guess you like

Origin www.cnblogs.com/ftxy/p/11715194.html
Recommended