Exemple #1
0
        private static void queryData(object source, ElapsedEventArgs e)
        {
            if (smartParams.Count > 0 && !disableWmi)
            {
                SmartTools.loadSmartData(disk, !fourBytes);
            }

            StringBuilder logstring = new StringBuilder();

            logstring.Append((DateTime.Now - start_time).TotalSeconds.ToString(us)).Append(",");
            logstring.Append(worker.GetWrittenMBytes().ToString(us)).Append(",");
            logstring.Append(worker.GetPerformance().ToString(us)).Append(",");
            logstring.Append(worker.GetInstPerformance().ToString(us)).Append(",");

            if (smartParams.Count > 0 && !disableWmi)
            {
                for (int i = 0; i < smartParams.Count; i++)
                {
                    logstring.Append(disk.smartData[smartParams[i]]);
                    if (i < smartParams.Count - 1)
                    {
                        logstring.Append(",");
                    }
                }
            }
            else if (hdSentinelDriveId.Length > 0)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("HDSentinel.xml");
                XmlNode node = doc.DocumentElement.SelectSingleNode(
                    "/Hard_Disk_Sentinel/Physical_Disk_Information_Disk_" + hdSentinelDriveId +
                    "/Hard_Disk_Summary/Current_Temperature");
                logstring.Append(node.InnerText.Substring(0, node.InnerText.Length - 3));
            }
            else
            {
                logstring.Append("NO_DATA");
            }
            logstring.Append(Environment.NewLine);

            String s = logstring.ToString();

            logfile.Write(s);
            Console.Write(s);
        }
Exemple #2
0
        static bool parseParameters(string[] args)
        {
            bool   show_help         = false;
            int    timeout_default   = 2000;
            int    limit_default     = 1;
            long   blocksize_default = ParseSize("16M");
            long   testsize_default  = ParseSize("512M");
            string blocksize_str     = null;
            string testsize_str      = null;

            var p = new OptionSet()
            {
                { "d|drive=", "Drive letter to test (always required)",
                  v => driveLetter = v.ToUpper() },

                { "s|smart:", "SMART value to log / list available values",
                  v => smartParams.Add(v) },

                { "x|xml=", "Use HDSentinel.xml, specify drive Id to use",
                  v => hdSentinelDriveId = v },

                { "w|disablewmi", "Do not query WMI",
                  v => disableWmi = v != null },

                { "4|four", "Interpret SMART values as 4 (not 2) bytes",
                  v => fourBytes = v != null },

                { "i|interval=",
                  String.Format("Timeout between measurements (in ms)\nMust be >= 500 (default {0})",
                                timeout_default),
                  (int v) => timeOut = v },

                { "l|limit=",
                  String.Format("Time in minutes to run the test\n(0 = run until stopped, default {0})",
                                limit_default),
                  (int v) => howLong = v },

                { "b|blocksize=",
                  String.Format("Blocksize in K/M/G Byte (default {0})",
                                FormatSize(blocksize_default)),
                  v => blocksize_str = v },

                { "t|testsize=",
                  String.Format("Testsize in K/M/G Byte (default {0})",
                                FormatSize(testsize_default)),
                  v => testsize_str = v },

                { "o|output=", "Output CSV file name",
                  v => outputFile = v },

                { "h|help", "Show help",
                  v => show_help = v != null },
            };

            try
            {
                p.Parse(args);
                if (driveLetter == null || driveLetter.Length != 1)
                {
                    show_help = true;
                }
                if (timeOut < 500)
                {
                    timeOut = timeout_default;
                }
                if (howLong < 0)
                {
                    show_help = true;
                }

                if (blocksize_str == null)
                {
                    blocksize_b = blocksize_default;
                }
                else
                {
                    blocksize_b = ParseSize(blocksize_str);
                    if (blocksize_b <= 0)
                    {
                        Console.WriteLine("Invalid blocksize");
                        show_help = true;
                    }
                }

                if (testsize_str == null)
                {
                    testsize_b = testsize_default;
                }
                else
                {
                    testsize_b = ParseSize(testsize_str);
                    if (testsize_b <= 0)
                    {
                        Console.WriteLine("Invalid testsize");
                        show_help = true;
                    }
                }
            }
            catch (OptionException e)
            {
                show_help = true;
            }

            if (disableWmi && smartParams.Count > 0)
            {
                Console.WriteLine("WMI is disabled, cannot read SMART values.");
                Console.WriteLine("Please remove all SMART parameters and/or use the XML option.");
                show_help = true;
            }

            if (!show_help)
            {
                try
                {
                    disk = WmiTools.getDisk(driveLetter);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Could not open drive " + driveLetter + ":");
                    Console.WriteLine("Error Message: " + e.Message);
                    WmiTools.ListAllDisks();
                    show_help = true;
                }
            }

            if (!show_help && !disableWmi)
            {
                try
                {
                    SmartTools.loadSmartData(disk, !fourBytes);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Could not query WMI data (run as administrator?)");
                    Console.WriteLine("Error Message: " + e.Message);
                    show_help = true;
                }
            }

            if (!show_help)
            {
                long free;
                long total;
                disk.QuerySpace(out free, out total);
                if (testsize_b > free)
                {
                    Console.WriteLine(
                        String.Format("ERROR: Testsize ({0}) larger than free space on drive ({1})",
                                      FormatSize(testsize_b), FormatSize(free)));
                    show_help = true;
                }
            }

            if (!show_help && smartParams.Count > 0)
            {
                foreach (var par in smartParams)
                {
                    if (!disk.smartData.ContainsKey(par))
                    {
                        Console.WriteLine("ERROR: Smart parameter not found: " + par);
                        Console.WriteLine();
                        smartParams.Clear();
                        break;
                    }
                }
            }

            if (!show_help && smartParams.Count == 0 && hdSentinelDriveId.Length == 0 && !disableWmi)
            {
                Console.WriteLine("Listing available SMART parameters");
                Console.WriteLine();
                Console.WriteLine(disk);
                show_help = true;
            }

            if (!show_help)
            {
                if (outputFile == null)
                {
                    int i = 0;
                    do
                    {
                        outputFile = Regex.Replace(disk.productName, @"[^A-Za-z0-9]+", "_")
                                     + "_" + i + ".csv";
                        i++;
                    } while (File.Exists(outputFile));
                }
                try
                {
                    logfile = new StreamWriter(outputFile);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Could not open output file for writing");
                    Console.WriteLine("Error Message: " + e.Message);
                    show_help = true;
                }
            }

            if (show_help)
            {
                ShowHelp(p);
                Console.WriteLine();
                Console.WriteLine("Press any key to exit.");
                Console.ReadKey(true);
            }

            return(!show_help);
        }