private static void RunScriptAndEmail( decimal currentState, decimal limit, ThresholdType limitType, string scriptPath, string watcherName, bool isWarning) { //NOTE: isWarning == false means that notification is about watcher back within limits string[] scriptParts = scriptPath.Split(new[] { ' ' }, 2); ProcessStartInfo scriptInfo = new ProcessStartInfo( scriptParts[0], scriptParts[1] .Replace("%value%", currentState.ToString("####.#").Replace(",", ".")) .Replace("%limit%", limit.ToString("####.#").Replace(",", ".")) .Replace("%type%", limitType.ToString().ToLower()) ); try { Process.Start(scriptInfo); } catch (Exception ex) { Console.WriteLine($"Script start failed. Message: {ex.Message}"); } if (_emailNotifier.IsConfigured) { try { _emailNotifier.SendMail( watcherName, currentState, limit, isWarning ? EventType.Warning : EventType.Ok); } catch (Exception ex) { Console.WriteLine($"Email send failed. Message: {ex.Message}"); } } }
internal TimeSpan GetTimeSpanToUse(int threshold, ThresholdType thresholdType) { if (thresholdType == ThresholdType.Seconds) { return(TimeSpan.FromSeconds(threshold)); } if (thresholdType == ThresholdType.Hours) { return(TimeSpan.FromHours(threshold)); } if (thresholdType == ThresholdType.Minutes) { return(TimeSpan.FromMinutes(threshold)); } throw new InvalidThresholdTypeException(thresholdType.ToString()); }
private static void PrintConfig() { Console.WriteLine($"{new string('-', 16)}"); Console.WriteLine($"Checkup interval: {_tmrIntervalSeconds / 60} minute(s)"); if (_certificateWatcherIsConfigured) { Console.WriteLine($"Certificates checkup interval: {_certificcateCheckIntervalSeconds / 60 / 60 / 24} day(s)"); } Console.WriteLine($"Watched objects:{Environment.NewLine}"); foreach (KeyValuePair <string, decimal> disks in _watchedDisksThresholds) { Console.WriteLine( $"Disk {disks.Key}{Environment.NewLine}\t{_watchedDisksThresholdTypes[disks.Key].ToString()} space threshold {disks.Value} GB{Environment.NewLine}\tScript string {_watchedDisksScripts[disks.Key]}"); } Console.WriteLine( _virtualMemoryIsConfigured ? $"Virtual memory:{Environment.NewLine}\t{_virtualMemoryThresholdType.ToString()} space threshold {_virtualMemoryThresholdGb} GB{Environment.NewLine}\tScript string {_virtualMemoryThresholdScript}" : "Virtual memory watcher offline!"); if (_certificateWatcherIsConfigured) { Console.WriteLine($"{Environment.NewLine}Certificates:{Environment.NewLine}"); int i = 0; foreach (KeyValuePair <string, X509Certificate2> certificates in _watchedCertificates) { Console.WriteLine( $"{++i}) {certificates.Value.SubjectName.Name}{Environment.NewLine}\tDays before alert {_watchedCertificatesDaysBeforeAlert[certificates.Key]}.{Environment.NewLine}\tScript string {_watchedCertificatesScripts[certificates.Key]}{Environment.NewLine}"); } } else { Console.WriteLine("Certificate watcher is offline!"); } Console.WriteLine($"{new string('-', 16)}{Environment.NewLine}"); }