/// <summary>
        /// 获取系统负载率
        /// </summary>
        /// <returns></returns>
        public static LoadAverage GetLoadAverage()
        {
            try
            {
                string std = "";
                string err = "";
                string cmd = "top - bn1 | awk '/average:/ '";

                var ret = ProcessShell.Run(cmd, 1000, out std, out err);
                if (ret)
                {
                    if (!string.IsNullOrEmpty(std))
                    {
                        int pos_start = 0;
                        pos_start = std.IndexOf("load average:", StringComparison.Ordinal);
                        if (pos_start > 0)
                        {
                            std = std.Substring(pos_start + "load average:".Length).Trim();
                        }


                        string[] loadaverageinfo = std.Split(',');
                        if (loadaverageinfo.Length == 3)
                        {
                            var loadAverage = new LoadAverage();
                            loadAverage.LoadAverageMin1  = float.Parse((loadaverageinfo[0]).Trim());
                            loadAverage.LoadAverageMin5  = float.Parse((loadaverageinfo[1]).Trim());
                            loadAverage.LoadAverageMin15 = float.Parse((loadaverageinfo[2]).Trim());
                            return(loadAverage);
                        }
                    }

                    if (!string.IsNullOrEmpty(err))
                    {
                        int pos_start = 0;
                        pos_start = err.IndexOf("load average:", StringComparison.Ordinal);
                        if (pos_start > 0)
                        {
                            err = err.Substring(pos_start + "load average:".Length).Trim();
                        }

                        string[] loadaverageinfo = err.Split(',');
                        if (loadaverageinfo.Length == 3)
                        {
                            var loadAverage = new LoadAverage();
                            loadAverage.LoadAverageMin1  = float.Parse((loadaverageinfo[0]).Trim());
                            loadAverage.LoadAverageMin5  = float.Parse((loadaverageinfo[1]).Trim());
                            loadAverage.LoadAverageMin15 = float.Parse((loadaverageinfo[2]).Trim());
                            return(loadAverage);
                        }
                    }
                }
            }
            catch
            {
                return(null);
            }

            return(null);
        }
Exemple #2
0
        /// <summary>
        /// 获取pid
        /// </summary>
        /// <param name="processName"></param>
        /// <returns></returns>
        public static int GetProcessPid(string processName)
        {
            string cmd = "";

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                cmd = "ps -aux |grep " + processName + "|grep -v grep|awk \'{print $2}\'";
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                cmd = "ps -A |grep " + processName + "|grep -v grep|awk \'{print $1}\'";
            }

            ProcessShell.Run(cmd, 1000, out string std, out string err);
            if (string.IsNullOrEmpty(std) && string.IsNullOrEmpty(err))
            {
                return(-1);
            }

            int pid = -1;

            if (!string.IsNullOrEmpty(std))
            {
                int.TryParse(std, out pid);
            }

            if (!string.IsNullOrEmpty(err))
            {
                int.TryParse(err, out pid);
            }

            return(pid);
        }
        /// <summary>
        /// 获取cpu使用率和空闲率
        /// </summary>
        /// <returns></returns>
        public static CPUInfo GetCPUused()
        {
            try
            {
                string std = "";
                string err = "";
                string cmd = "top -bn1 | awk '/%Cpu/ {print $2+$4,$8}'";
                var    ret = ProcessShell.Run(cmd, 1000, out std, out err);
                if (ret)
                {
                    if (!string.IsNullOrEmpty(std))
                    {
                        string[] cpuinfo = std.Split(' ');
                        if (cpuinfo.Length == 2)
                        {
                            CPUInfo result = new CPUInfo();
                            result.CpuIdle = float.Parse(cpuinfo[1]);
                            result.CpuUsed = float.Parse(cpuinfo[0]);
                            return(result);
                        }
                    }

                    if (!string.IsNullOrEmpty(err))
                    {
                        string[] cpuinfo = err.Split(' ');
                        if (cpuinfo.Length == 2)
                        {
                            CPUInfo result = new CPUInfo();
                            result.CpuIdle = float.Parse(cpuinfo[1]);
                            result.CpuUsed = float.Parse(cpuinfo[0]);
                            return(result);
                        }
                    }
                }
            }
            catch
            {
                return(null);
            }

            return(null);
        }
Exemple #4
0
        private static bool ifNotMp4(string ffmpegBinPath, string videoFilePath, out string videoPath)
        {
            string ext         = Path.GetExtension(videoFilePath);
            string newFileName = videoFilePath.Replace(ext, ".mp4");
            string ffmpegCmd   = ffmpegBinPath + " -i " + videoFilePath + " -c copy -movflags faststart " +
                                 newFileName;

            videoPath = newFileName;


            if (!string.IsNullOrEmpty(ext) && !ext.Trim().ToLower().Equals(".mp4"))
            {
                if (ProcessShell.Run(ffmpegCmd, 60 * 1000 * 5, out string std, out string err))
                {
                    if (!string.IsNullOrEmpty(std) || !string.IsNullOrEmpty(err))
                    {
                        if (File.Exists(newFileName))
                        {
                            FileInfo fi = new FileInfo(newFileName);
                            if (fi.Length > 100)
                            {
                                File.Delete(videoFilePath);
                                return(true);
                            }

                            return(false);
                        }

                        return(false);
                    }

                    return(false);
                }
            }

            return(true);
        }
Exemple #5
0
        /// <summary>
        /// 输出视频的时长(毫秒)
        /// </summary>
        /// <param name="ffmpegBinPath"></param>
        /// <param name="videoFilePath"></param>
        /// <param name="duartion"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public static bool GetDuration(string ffmpegBinPath, string videoFilePath, out long duartion, out string path)
        {
            duartion = -1;
            if (File.Exists(ffmpegBinPath) && File.Exists(videoFilePath))
            {
                string newPath = "";
                var    ret     = ifNotMp4(ffmpegBinPath, videoFilePath, out newPath);
                if (ret)
                {
                    videoFilePath = newPath;
                }

                path = videoFilePath;
                string cmd = ffmpegBinPath + " -i " + videoFilePath;
                if (ProcessShell.Run(cmd, 1000, out string std, out string err))
                {
                    if (!string.IsNullOrEmpty(std) || !string.IsNullOrEmpty(err))
                    {
                        string tmp = "";
                        if (!string.IsNullOrEmpty(std))
                        {
                            tmp = Common.GetValue(std, "Duration:", ",");
                        }

                        if (string.IsNullOrEmpty(tmp))
                        {
                            tmp = Common.GetValue(err, "Duration:", ",");
                        }

                        if (!string.IsNullOrEmpty(tmp))
                        {
                            string[] tmpArr = tmp.Split(':', StringSplitOptions.RemoveEmptyEntries);
                            if (tmpArr.Length == 3)
                            {
                                int hour = int.Parse(tmpArr[0]);
                                int min  = int.Parse(tmpArr[1]);
                                int sec  = 0;
                                int msec = 0;
                                if (tmpArr[2].Contains('.'))
                                {
                                    string[] tmpArr2 = tmpArr[2].Split('.', StringSplitOptions.RemoveEmptyEntries);
                                    sec  = int.Parse(tmpArr2[0]);
                                    msec = int.Parse(tmpArr2[1]);
                                }
                                else
                                {
                                    sec = int.Parse(tmpArr[2]);
                                }

                                hour     = hour * 3600;              //换成秒数
                                min      = min * 60;
                                sec      = sec + hour + min;         //合计秒数
                                duartion = sec * 1000 + (msec * 10); //算成毫秒

                                /*
                                 * LogWriter.WriteLog("获取视频时长:" + duartion.ToString() + "毫秒", videoFilePath);
                                 */
                                return(true);
                            }
                        }
                    }
                }
            }

            path = videoFilePath;
            return(false);
        }
Exemple #6
0
        public static void KillProcess(int pid)
        {
            string cmd = "kill -9 " + pid.ToString();

            ProcessShell.Run(cmd, 1000);
        }