Windows Services, Diskspace, Scheduled Job and URL monitoring in C#

Windows Services, Diskspace, Scheduled Job and URL monitoring in C#: 

 XmlDocument doc = new XmlDocument();
            doc.Load("../../Services_File.xml");
            XmlNodeList nodeList = doc.DocumentElement.SelectNodes("/WindowsServices/ServiceList");
            string server = "", services = "";
            foreach (XmlNode spcnode in nodeList)
            {
                server = spcnode.SelectSingleNode("Server").InnerText;
                services = spcnode.SelectSingleNode("Services").InnerText;
                string[] svcList = services.Split(';');
                foreach(string svc in svcList)
                    CheckService(server, svc);
               
            }
            doc.Load("../../DiskSpace_File.xml");
            XmlNodeList spaceList = doc.DocumentElement.SelectNodes("/DiskSpace/ServerList");
            string serverd = "", disksd = "";
            foreach (XmlNode drvnode in spaceList)
            {
                serverd = drvnode.SelectSingleNode("Server").InnerText;
                disksd = drvnode.SelectSingleNode("Drives").InnerText;
                string[] drvList = disksd.Split(';');
                foreach (string drv in drvList)
                    CheckDiskSpace(serverd, drv);
            }

            ConnectionOptions conOptions = new ConnectionOptions()
            {
                EnablePrivileges = true
            };

            ManagementScope scope = new ManagementScope("\\\\CSCINDAH503164.asiapac.globalcsc.net\\root\\cimv2", conOptions);
            ObjectQuery query = new ObjectQuery(string.Format("SELECT * FROM Win32_ScheduledJob"));
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
            ManagementObjectCollection coll = searcher.Get();

            foreach (var r in coll)
            {

                Console.WriteLine(r["Name"].ToString() + ": " + r["Description"].ToString() + ": " + r["Status"].ToString() + ": " + r["JobStatus"].ToString()+": "+r["ElapsedTime"].ToString());
                //Console.ReadLine();
            }
           
            Console.ReadLine();
                     
            WebHeaderCollection headers = new WebHeaderCollection();
           
            ServicePointManager.ServerCertificateValidationCallback = ((object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors policyerrors) => true);
           
            doc.Load("../../URLs_File.xml");
            XmlNode urlnode = doc.DocumentElement.SelectSingleNode("/URLS");
            string[] urls = urlnode.InnerText.ToString().Split(';');
            foreach (string url in urls)
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "GET";
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                Console.WriteLine(resp.StatusCode.ToString());
                //Console.ReadLine();
            }
           
        }
        public static void CheckService(string servername, string servicename)
        {
            ConnectionOptions conOptions = new ConnectionOptions()
            {
                EnablePrivileges = true
            };

            ManagementScope scope = new ManagementScope("\\\\"+servername+"\\root\\cimv2", conOptions);
            ObjectQuery query = new ObjectQuery(string.Format("SELECT * FROM Win32_Service WHERE DisplayName = '{0}'", servicename));
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
            ManagementObjectCollection coll = searcher.Get();

            foreach (var r in coll)
            {

                Console.WriteLine(r["Name"].ToString() + ": " + r["StartMode"].ToString() + ": " + r["State"].ToString() + ": " + r["StartName"].ToString());
                //Console.ReadLine();
            }
        }
        public static void CheckDiskSpace(string servername, string drivename)
        {
            ConnectionOptions conOptions = new ConnectionOptions()
            {
                EnablePrivileges = true
            };

            ManagementScope scope = new ManagementScope("\\\\" + servername + "\\root\\cimv2", conOptions);
            ObjectQuery query = new ObjectQuery(string.Format("SELECT * FROM Win32_LogicalDisk WHERE DriveType=3 AND Name = '{0}:'", drivename));
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
            ManagementObjectCollection coll = searcher.Get();

            foreach (var r in coll)
            {

                Console.WriteLine(r["Name"].ToString() + ": " + Math.Round(Convert.ToDouble(r["FreeSpace"].ToString()) / 1073741824.0) + " GB: " + Math.Round(Convert.ToDouble(r["Size"].ToString()) / 1073741824.0) + " GB : % Free" + ((Math.Round(Convert.ToDouble(r["FreeSpace"].ToString()) / 1073741824.0)) / (Math.Round(Convert.ToDouble(r["Size"].ToString()) / 1073741824.0)))*100.0);
                //Console.ReadLine();
            }
        }

Comments

Popular posts from this blog

Base 64 encoding and decoding

LINQ Queries with GROUP BY, INNER JOIN, COUNT and SUM: Examples

How to write Custom delete Confirmation Modal for Kendo Grid in MVC: