public bool TryCreateHostGroup(string name) { using (var context = _contextCreator()) { try { var service = new ZabbixApi.Services.HostGroupService(context); var hostGroup = service.GetByName(name); if (hostGroup == null) { hostGroup = new HostGroup(); hostGroup.name = name; hostGroup.Id = service.Create(hostGroup); } if (hostGroup.Id != null) { _HostGroupMetricsCache = hostGroup; return(true); } else { return(false); } } catch (Exception ex) { MetricsErrorHandler.Handle(ex, string.Format("Error on configuring zabbix host group, zabbix api {0}", _url)); return(false); } } }
public bool TryCreateApplication(string name, string hostid) { using (var context = _contextCreator()) { try { var service = new ZabbixApi.Services.ApplicationService(context); var applications = service.Get(new { name = name, hostid = hostid }); var application = applications == null || applications.Count() == 0 ? null : applications.First(); if (application == null) { application = new Application(); application.hostid = hostid; application.name = name; application.Id = service.Create(application); } if (application.Id != null) { _ApplicationCache = application; return(true); } else { return(false); } } catch (Exception ex) { MetricsErrorHandler.Handle(ex, string.Format("Error on configuring zabbix application, zabbix api {0}", _url)); return(false); } }; }
private IEnumerable <Uri> ReadRemotesFromConfig() { if (!File.Exists(remotesFile)) { yield break; } var remotes = File.ReadAllLines("remotes.txt") .Where(l => !string.IsNullOrWhiteSpace(l)) .Where(l => !l.StartsWith("#")); foreach (var remote in remotes) { Uri uri = null; try { uri = new Uri(remote, UriKind.Absolute); } catch (Exception x) { MetricsErrorHandler.Handle(x, "Unable to read uri from remotes.txt file"); } if (uri != null) { yield return(uri); } } }
private void ProcessRequests() { while (!this.cts.IsCancellationRequested) { try { var context = TaskHelper.RunAsync <HttpListenerContext>(() => { return(this.httpListener.GetContext()); }, null).Result; try { using (timer.NewContext()) { TaskHelper.RunWithWait(() => { ProcessRequest(context); context.Response.Close(); }, null); } } catch (Exception ex) { errors.Mark(); context.Response.StatusCode = 500; context.Response.StatusDescription = "Internal Server Error"; context.Response.Close(); MetricsErrorHandler.Handle(ex, "Error processing HTTP request"); } } catch (Exception ex) { errors.Mark(); HttpListenerException httpException = ex as HttpListenerException; if (httpException == null || httpException.ErrorCode != 995)// IO operation aborted { MetricsErrorHandler.Handle(ex, "Error processing HTTP request"); } } } }
private static void RunScheduler(TimeSpan interval, Func <CancellationToken, Task> action, CancellationTokenSource token, int toleratedConsecutiveFailures) { Task.Factory.StartNew(async() => { var nbFailures = 0; while (!token.IsCancellationRequested) { try { await Task.Delay(interval, token.Token).ConfigureAwait(false); try { await action(token.Token).ConfigureAwait(false); nbFailures = 0; } catch (Exception x) { MetricsErrorHandler.Handle(x, "Error while executing action scheduler."); if (toleratedConsecutiveFailures >= 0) { nbFailures++; if (nbFailures > toleratedConsecutiveFailures) { token.Cancel(); } } } } catch (TaskCanceledException) { } } }, token.Token); }
private static void RunScheduler(TimeSpan interval, Func <CancellationToken, Task> action, CancellationTokenSource token) { Task.Factory.StartNew(() => { while (!token.IsCancellationRequested) { try { Task task = Task.Factory.StartNew(() => { Thread.Sleep(interval); }, token.Token); task.Wait(); //Task.Delay(interval, token.Token); try { action(token.Token); } catch (Exception x) { MetricsErrorHandler.Handle(x, "Error while executing action scheduler."); token.Cancel(); } } catch (TaskCanceledException) { } } }, token.Token); }
public bool TryCreateHost(string name) { ZabbixApi.Helper.Check.IsNotNullOrWhiteSpace(name, "name"); if (!TryCreateHostGroup("Metrics.NET")) { return(false); } using (var context = _contextCreator()) { try { var service = new ZabbixApi.Services.HostService(context); var host = service.GetByName(name); if (host == null) { if (IsIpAddress(name)) { host = new Host(); host.host = name; host.interfaces = host.interfaces ?? new List <HostInterface>(); host.interfaces.Add(new HostInterface() { type = HostInterface.InterfaceType.Agent, main = true, useip = true, ip = name, dns = "", port = "10050" }); host.groups = host.groups ?? new List <HostGroup>(); host.groups.Add(_HostGroupMetricsCache); host.Id = service.Create(host); } else { return(false); } } if (host.Id != null) { _HostCache = host; return(true); } else { return(false); } } catch (Exception ex) { MetricsErrorHandler.Handle(ex, string.Format("Error on create zabbix host, zabbix api {0}", _url)); return(false); } } }
public bool TryCreateTemplate(string name) { if (!TryCreateHostGroup("Metrics.NET")) { return(false); } using (var context = _contextCreator()) { try { var service = new ZabbixApi.Services.TemplateService(context); var template = service.GetByPropety("name", name); if (template == null) { template = new Template(); template.host = name; template.name = name; template.groups = new List <HostGroup> { _HostGroupMetricsCache }; template.hosts = new List <Host> { _HostCache }; template.Id = service.Create(template); } else { if (template.hosts.FirstOrDefault(h => h.Id == _HostCache.Id) == null) { template.hosts.Add(_HostCache); if (service.Update(template) == null) { return(false); } } } if (template.Id != null) { TryCreateApplication(name, template.Id); _TemplateCache = template; return(true); } else { return(false); } } catch (Exception ex) { MetricsErrorHandler.Handle(ex, string.Format("Error on configuring zabbix template, zabbix api {0}", _url)); return(false); } } }
private void RunAction(Action <CancellationToken> action) { try { action(this.token.Token); } catch (Exception x) { MetricsErrorHandler.Handle(x, "Error while executing scheduled action."); } }
private static string SafeGetString(Func <string> action) { try { return(action()); } catch (Exception x) { MetricsErrorHandler.Handle(x, "Error retrieving environment value"); return(string.Empty); } }
public override void Flush() { try { this.client?.GetStream().Flush(); } catch (Exception x) { using (this.client) { } this.client = null; MetricsErrorHandler.Handle(x, "Error sending TCP data to graphite endpoint " + host + ":" + port.ToString()); } }
/// <summary> /// Writes the byte array to the InfluxDB server in a single UDP send operation. /// </summary> /// <param name="bytes">The bytes to write to the InfluxDB server.</param> /// <returns>The HTTP response from the server after writing the message.</returns> protected override Byte[] WriteToTransport(Byte[] bytes) { try { using (var client = new UdpClient()) { int result = client.Send(bytes, bytes.Length, config.Hostname, config.Port.Value); return(Encoding.UTF8.GetBytes(result.ToString())); } } catch (Exception ex) { String firstNLines = "\n" + String.Join("\n", Encoding.UTF8.GetString(bytes).Split('\n').Take(5)) + "\n"; MetricsErrorHandler.Handle(ex, $"Error while uploading {Batch.Count} measurements ({formatSize(bytes.Length)}) to InfluxDB over UDP [net.udp://{config.Hostname}:{config.Port.Value}/] - Ensure that the message size is less than the UDP send buffer size (usually 8-64KB), and reduce the BatchSize on the InfluxdbWriter if necessary. - First 5 lines: {firstNLines}"); return(Encoding.UTF8.GetBytes(0.ToString())); } }
/// <summary> /// Writes the byte array to the InfluxDB server in a single HTTP POST operation. /// </summary> /// <param name="bytes">The bytes to write to the InfluxDB server.</param> /// <returns>The HTTP response from the server after writing the message.</returns> protected override Byte[] WriteToTransport(Byte[] bytes) { try { using (var client = new WebClient()) { var result = client.UploadData(influxDbUri, bytes); return(result); } } catch (WebException ex) { String response = new StreamReader(ex.Response?.GetResponseStream() ?? Stream.Null).ReadToEnd(); String firstNLines = "\n" + String.Join("\n", Encoding.UTF8.GetString(bytes).Split('\n').Take(5)) + "\n"; MetricsErrorHandler.Handle(ex, $"Error while uploading {Batch.Count} measurements ({formatSize(bytes.Length)}) to InfluxDB over JSON HTTP [{influxDbUri}] [ResponseStatus: {ex.Status}] [Response: {response}] - First 5 lines: {firstNLines}"); return(Encoding.UTF8.GetBytes(response)); } }
protected override void EndReport(string contextName, DateTime timestamp) { try { File.WriteAllText(this.fileName, this.buffer.ToString()); } catch (Exception x) { MetricsErrorHandler.Handle(x, "Error writing text file " + this.fileName); } base.EndReport(contextName, timestamp); this.buffer = null; }
public PerformanceCounterGauge(string category, string counter, string instance) { try { this.performanceCounter = instance == null ? new PerformanceCounter(category, counter, true) : new PerformanceCounter(category, counter, instance, true); } catch (Exception x) { var message = "Error reading performance counter data. The application is currently running as user " + GetIdentity() + ". Make sure the user has access to the performance counters. The user needs to be either Admin or belong to Performance Monitor user group."; MetricsErrorHandler.Handle(x, message); } }
public ZabbixReporter(string zabbixServer, int port = 10051, string user = null, string password = null, string template = null) { template = template ?? GetGlobalContextName(); Hostname = ZabbixConfig.GetLocalHostname(); ZabbixSender = new ZabbixSender(zabbixServer, port); try { ZabbixConfig = new ZabbixConfig(zabbixServer, user, password); ZabbixConfig.TryCreateTemplate(template); } catch (Exception ex) { MetricsErrorHandler.Handle(ex); } }
public bool TryCreateTrapperItem(string key, string units = "", Item.ValueType valueType = Item.ValueType.NumericUnsigned, Item.DataType dataType = Item.DataType.Decimal) { if (_ItemsCache.ContainsKey(key)) { return(true); } using (var context = _contextCreator()) { try { var service = new ItemService(context); var items = service.Get(new { key_ = key, hostid = _TemplateCache.Id }); var item = items == null || items.Count() == 0 ? null : items.First(); if (item == null) { item = new Item(); item.name = key; item.key_ = key; item.type = Item.ItemType.ZabbixTrapper; item.value_type = valueType; if (valueType == Item.ValueType.NumericUnsigned) { item.data_type = dataType; } item.units = units; item.hostid = _TemplateCache.Id; var addition = new Dictionary <string, object>(); addition["applications"] = new string[] { _ApplicationCache.Id }; item.Id = service.Create(item, addition); } if (item.Id != null) { _ItemsCache[key] = item; return(true); } else { return(false); } } catch (Exception ex) { MetricsErrorHandler.Handle(ex, string.Format("Error on configuring zabbix trapper item, zabbix api {0}", _url)); return(false); } } }
public override void Flush() { try { WriteCurrentJar(); if (this.client != null) { this.client.GetStream().Flush(); } } catch (Exception x) { using (this.client) { } this.client = null; MetricsErrorHandler.Handle(x, "Error sending Pickled data to graphite endpoint " + host + ":" + port.ToString()); } }
private void UpdateMetrics(Uri remoteUri, Func <string, JsonMetricsContext> deserializer, CancellationToken token) { try { var remoteContext = Metrics.Visualization.TaskHelper.RunAsync <JsonMetricsContext>(() => { return(HttpRemoteMetrics.FetchRemoteMetrics(remoteUri, deserializer, token)); }, null).Result; remoteContext.Environment.Add("RemoteUri", remoteUri.ToString()); remoteContext.Environment.Add("RemoteVersion", remoteContext.Version); remoteContext.Environment.Add("RemoteTimestamp", remoteContext.Timestamp); this.currentData = remoteContext.ToMetricsData(); } catch (Exception x) { MetricsErrorHandler.Handle(x, "Error updating metrics data from " + remoteUri.ToString()); this.currentData = MetricsData.Empty; } }
private async Task ProcessRequests() { while (!this.cts.IsCancellationRequested) { try { var context = await this.httpListener.GetContextAsync(); try { using (timer.NewContext()) { await ProcessRequest(context).ConfigureAwait(false); context.Response.Close(); } } catch (Exception ex) { errors.Mark(); context.Response.StatusCode = 500; context.Response.StatusDescription = "Internal Server Error"; context.Response.Close(); MetricsErrorHandler.Handle(ex, "Error processing HTTP request"); } } catch (ObjectDisposedException ex) { if ((ex.ObjectName == this.httpListener.GetType().FullName) && (this.httpListener.IsListening == false)) { return; // listener is closed/disposed } MetricsErrorHandler.Handle(ex, "Error processing HTTP request"); } catch (Exception ex) { errors.Mark(); HttpListenerException httpException = ex as HttpListenerException; if (httpException == null || httpException.ErrorCode != 995)// IO operation aborted { MetricsErrorHandler.Handle(ex, "Error processing HTTP request"); } } } }
private async Task UpdateMetrics(Uri remoteUri, Func <string, JsonMetricsContext> deserializer, CancellationToken token) { try { var remoteContext = await HttpRemoteMetrics.FetchRemoteMetrics(remoteUri, deserializer, token).ConfigureAwait(false); remoteContext.Environment.Add("RemoteUri", remoteUri.ToString()); remoteContext.Environment.Add("RemoteVersion", remoteContext.Version); remoteContext.Environment.Add("RemoteTimestamp", Clock.FormatTimestamp(remoteContext.Timestamp)); this.currentData = remoteContext.ToMetricsData(); } catch (Exception x) { MetricsErrorHandler.Handle(x, "Error updating metrics data from " + remoteUri.ToString()); this.currentData = MetricsData.Empty; } }
private void WriteCurrentJar() { try { if (this.client == null) { this.client = InitClient(this.host, this.port); } this.jar.WritePickleData(this.client.GetStream()); } catch (Exception x) { using (this.client) { } this.client = null; MetricsErrorHandler.Handle(x, "Error sending Pickled data to graphite endpoint " + host + ":" + port.ToString()); } }
protected override void SendData(string data) { try { if (this.client == null) { this.client = InitClient(this.host, this.port); } var bytes = Encoding.UTF8.GetBytes(data); this.client.Send(bytes, bytes.Length); } catch (Exception x) { using (this.client) { } this.client = null; MetricsErrorHandler.Handle(x, "Error sending UDP data to graphite endpoint " + host + ":" + port.ToString()); } }
/// <summary> /// Flushes all buffered records in the batch by writing them to the server in a single write operation. /// </summary> public virtual void Flush() { if (Batch.Count == 0) { return; } Byte[] bytes = new Byte[0]; try { bytes = GetBatchBytes(Batch); WriteToTransport(bytes); } catch (Exception ex) { String firstNLines = "\n" + String.Join("\n", Encoding.UTF8.GetString(bytes).Split('\n').Take(5)) + "\n"; MetricsErrorHandler.Handle(ex, $"Error while flushing {Batch.Count} measurements to InfluxDB. Bytes: {formatSize(bytes.Length)} - First 5 lines: {firstNLines}"); } finally { // clear always, regardless if it was successful or not Batch.Clear(); } }
public void Send(DataPointUploadMessage msg) { try { var request = _requestor.GetRequestor(); using (var rs = request.GetWriteStream()) { Serializer.Serialize(rs, msg); // flush the message before disposing rs.Flush(); } try { using (request.Send()) { } } catch (SecurityException) { log.Error("API token for sending metrics to SignalFuse is invalid"); } } catch (Exception ex) { if (ex is WebException) { var webex = ex as WebException; using (var exresp = webex.Response) { if (exresp != null) { var stream2 = exresp.GetResponseStream(); var reader2 = new StreamReader(stream2); var errorStr = reader2.ReadToEnd(); log.Error(errorStr); } } } MetricsErrorHandler.Handle(ex, "Failed to send metrics"); } }
private static void Register(this MetricsContext context, string name, Unit unit, string category, string counter, string instance = null, Func <double, double> derivate = null, MetricTags tags = default(MetricTags)) { try { WrappedRegister(context, name, unit, category, counter, instance, derivate, tags); } catch (UnauthorizedAccessException x) { var message = "Error reading performance counter data. The application is currently running as user " + GetIdentity() + ". Make sure the user has access to the performance counters. The user needs to be either Admin or belong to Performance Monitor user group."; MetricsErrorHandler.Handle(x, message); } catch (Exception x) { var message = "Error reading performance counter data. The application is currently running as user " + GetIdentity() + ". Make sure the user has access to the performance counters. The user needs to be either Admin or belong to Performance Monitor user group."; MetricsErrorHandler.Handle(x, message); } }
public static Task <MetricsHttpListener> StartHttpListenerAsync(string httpUriPrefix, MetricsDataProvider dataProvider, Func <HealthStatus> healthStatus, CancellationToken token, int maxRetries = 1) { return(Task.Factory.StartNew(async() => { MetricsHttpListener listener = null; var remainingRetries = maxRetries; do { try { listener = new MetricsHttpListener(httpUriPrefix, dataProvider, healthStatus, token); listener.Start(); if (remainingRetries != maxRetries) { log.InfoFormat("HttpListener started successfully after {0} retries", maxRetries - remainingRetries); } remainingRetries = 0; } catch (Exception x) { using (listener) { } listener = null; remainingRetries--; if (remainingRetries > 0) { log.WarnException("Unable to start HTTP Listener. Sleeping for {0} sec and retrying {1} more times", x, maxRetries - remainingRetries, remainingRetries); await Task.Delay(1000 * (maxRetries - remainingRetries), token).ConfigureAwait(false); } else { MetricsErrorHandler.Handle(x, $"Unable to start HTTP Listener. Retried {maxRetries} times, giving up..."); } } } while (remainingRetries > 0); return listener; }, token).Unwrap()); }
private static void HandleHttpError(Exception exception) { MetricsErrorHandler.Handle(exception, "Error processing HTTP request"); }