远程监控linux系统信息内存,cpu

一、主要指令

当需要监控linux的系统运行状态时,不免要获取部分系统信息如内存cpu等,主要指令有:

1.top 用于获取cpu等信息,类似于windows上面的任务管理器

2.free 用于获取内存信息,包括可用内存等

3.df用于获取瓷盘信息,如瓷盘的使用空间和剩余空间等

二、命令执行

这些指令要如何通过windows上的程序下发到Linux执行并获取返回值呢,这里需要用到ssh.net库,具体用法详见代码。

三、权限管理

如果不以root登录的话,是没有权限执行重启等操作的的。那么,如何以远程方式登录root账户呢?这就需要在ssh中配置允许以root账户登录:

方法如下:

vi /etc/ssh/sshd_config,将PermitRootLogin设置为yes。如果没有该项的话,向其添加:

PermitRootLogin yes

2. 重启ssh服务

Service sshd restart 

若不行的话就重启机器。

    
        /// <summary>
        /// 获取linux内存cpu磁盘信息
        /// </summary>
        /// <returns></returns>
        static string GetTerminalSystemInfo()
        {
            string cpucommand = "top -bn 1 -i -c|grep %Cpu";
            string memcommand = "free -m | grep cache:";
            string discommand = "df -h | grep sda1";
            try
            {
                using (var client = new SshClient(server, username, password))
                {
                    try
                    {
                        client.Connect();
                        string cpuStr = client.RunCommand(cpucommand).Execute();
                        string splitor = "ni,";
                        string a = cpuStr.Substring(cpuStr.IndexOf(splitor) + splitor.Length).Trim(' ');
                        string b = a.Remove(a.IndexOf("id,")).Trim(' ');//cpu 空闲率

                        string memStr = client.RunCommand(memcommand).Execute();
                        splitor = "-/+ buffers/cache:";
                        a = memStr.Substring(memStr.IndexOf(splitor) + splitor.Length).Trim(' ');
                        string c = a.Remove(a.IndexOf(" ")).Trim(' ');//mem usage

                        string d = a.Substring(a.IndexOf(" ") + 1).Trim(' ').TrimEnd(Environment.NewLine.ToCharArray());//去掉换行符

                        string diskStr = client.RunCommand(discommand).Execute();
                        splitor = "/dev/sda1";
                        a = diskStr.Substring(diskStr.IndexOf(splitor) + splitor.Length).Trim(' ');
                        string[] arr =  a.Split(' ');

                        //linq
                        List<string> numArr = arr.Where(x => !x.Equals(string.Empty)).Select(x => x).ToList();

                        //数组
                        //List<string> numArr = new List<string>();
                        //foreach(var item in arr)
                        //{
                        //    if(!item.Equals(string.Empty))
                        //    {
                        //        numArr.Add(item);
                        //    }
                        //}

                        double fff = 100 - Convert.ToDouble(b);
                        client.Disconnect();
                        return string.Format("CPU使用率:" + fff.ToString("F2") + "% 内存使用率:" + c + "MB" + " 内存剩余量:" + d + "MB." + "磁盘总量:" + numArr[0] + " 磁盘使用率:" + numArr[3]);
                    }
                    catch (Exception ex)
                    {
                        return "获取系统信息失败,原因:" + ex.Message;
                    }
                }
            }
            catch (Exception ex)
            {
                return "获取系统信息失败,原因:" + ex.Message;
            }
        }

        /// <summary>
        /// 重启linux
        /// </summary>
        /// <returns></returns>
        static string RestartTerminal()
        {
            string restartCommand = "reboot";
            try
            {
                Task.Run(() =>
                {
                    using (var client = new SshClient(server, username, password))
                    {
                        try
                        {
                            client.Connect();
                            client.RunCommand(restartCommand).Execute();
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.ToString());
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 终端重启失败,原因:" + ex.Message;
            }
            return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 终端重启完成!";
        }
    }



猜你喜欢

转载自blog.csdn.net/u010178308/article/details/80352646