From 2117fce3894d8b9388960848688f8da2ad203066 Mon Sep 17 00:00:00 2001 From: stop-cran Date: Sat, 1 Dec 2018 14:08:44 +0200 Subject: [PATCH] First version. --- .gitattributes | 63 +++++ .gitignore | 261 +++++++++++++++++++ README.md | 20 ++ ZabbixSender.Async.sln | 25 ++ ZabbixSender.Async/SendData.cs | 22 ++ ZabbixSender.Async/Sender.cs | 135 ++++++++++ ZabbixSender.Async/SenderResponse.cs | 9 + ZabbixSender.Async/ZabbixSender.Async.csproj | 11 + 8 files changed, 546 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 README.md create mode 100644 ZabbixSender.Async.sln create mode 100644 ZabbixSender.Async/SendData.cs create mode 100644 ZabbixSender.Async/Sender.cs create mode 100644 ZabbixSender.Async/SenderResponse.cs create mode 100644 ZabbixSender.Async/ZabbixSender.Async.csproj diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..1ff0c42 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,63 @@ +############################################################################### +# Set default behavior to automatically normalize line endings. +############################################################################### +* text=auto + +############################################################################### +# Set default behavior for command prompt diff. +# +# This is need for earlier builds of msysgit that does not have it on by +# default for csharp files. +# Note: This is only used by command line +############################################################################### +#*.cs diff=csharp + +############################################################################### +# Set the merge driver for project and solution files +# +# Merging from the command prompt will add diff markers to the files if there +# are conflicts (Merging from VS is not affected by the settings below, in VS +# the diff markers are never inserted). Diff markers may cause the following +# file extensions to fail to load in VS. An alternative would be to treat +# these files as binary and thus will always conflict and require user +# intervention with every merge. To do so, just uncomment the entries below +############################################################################### +#*.sln merge=binary +#*.csproj merge=binary +#*.vbproj merge=binary +#*.vcxproj merge=binary +#*.vcproj merge=binary +#*.dbproj merge=binary +#*.fsproj merge=binary +#*.lsproj merge=binary +#*.wixproj merge=binary +#*.modelproj merge=binary +#*.sqlproj merge=binary +#*.wwaproj merge=binary + +############################################################################### +# behavior for image files +# +# image files are treated as binary by default. +############################################################################### +#*.jpg binary +#*.png binary +#*.gif binary + +############################################################################### +# diff behavior for common document formats +# +# Convert binary document formats to text before diffing them. This feature +# is only available from the command line. Turn it on by uncommenting the +# entries below. +############################################################################### +#*.doc diff=astextplain +#*.DOC diff=astextplain +#*.docx diff=astextplain +#*.DOCX diff=astextplain +#*.dot diff=astextplain +#*.DOT diff=astextplain +#*.pdf diff=astextplain +#*.PDF diff=astextplain +#*.rtf diff=astextplain +#*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c4efe2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,261 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +project.fragment.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +#*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..639ce06 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# Synopsis + +This package provide a tool to send data to Zabbix in the same way as zabbix_sender tool. It implements [Zabbix Sender Protocol 4.0](https://www.zabbix.org/wiki/Docs/protocols/zabbix_sender/4.0). + +# Installation + +NuGet package is available [here](https://www.nuget.org/packages/ZabbixSender.Async/). + +```PowerShell +PM> Install-Package ZabbixSender.Async +``` + +# Example + +```C# +var sender = new ZabbixSender.Async.Sender("192.168.0.10"); +var response = sender.Send("MonitoredHost1", "trapper.item1", "10"); +Console.WriteLine(reponse.Response); // "success" or "fail" +Console.WriteLine(response.Info); // e.g. "Processed 1 Failed 0 Total 1 Seconds spent 0.000253" +``` \ No newline at end of file diff --git a/ZabbixSender.Async.sln b/ZabbixSender.Async.sln new file mode 100644 index 0000000..c8f0a89 --- /dev/null +++ b/ZabbixSender.Async.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.136 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZabbixSender.Async", "ZabbixSender.Async\ZabbixSender.Async.csproj", "{4D75CEC8-46DC-4959-8F60-94AF4565B7A5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4D75CEC8-46DC-4959-8F60-94AF4565B7A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4D75CEC8-46DC-4959-8F60-94AF4565B7A5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4D75CEC8-46DC-4959-8F60-94AF4565B7A5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4D75CEC8-46DC-4959-8F60-94AF4565B7A5}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E284DD63-F6DB-4DF9-9D29-6462C73C1B70} + EndGlobalSection +EndGlobal diff --git a/ZabbixSender.Async/SendData.cs b/ZabbixSender.Async/SendData.cs new file mode 100644 index 0000000..3de6e10 --- /dev/null +++ b/ZabbixSender.Async/SendData.cs @@ -0,0 +1,22 @@ +namespace ZabbixSender.Async +{ + public class SendData + { + /// + /// Host name to monitor. Should be configured in Zabbix. + /// + public string Host { get; set; } + + /// + /// A key of the item to send. + /// Should belong to the specified host and have "Zabbix sender" type. + /// + public string Key { get; set; } + + /// + /// An item value. + /// Should be formatted in a way to respect the configured "type of information" of the item. + /// + public string Value { get; set; } + } +} \ No newline at end of file diff --git a/ZabbixSender.Async/Sender.cs b/ZabbixSender.Async/Sender.cs new file mode 100644 index 0000000..c72998b --- /dev/null +++ b/ZabbixSender.Async/Sender.cs @@ -0,0 +1,135 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; +using System; +using System.Collections.Generic; +using System.IO; +using System.Net.Sockets; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace ZabbixSender.Async +{ + public class Sender + { + private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); + + private static readonly byte[] ZabbixHeader = new byte[] { 0x5a, 0x42, 0x58, 0x44, 0x01 }; + + /// + /// Use this class to send data to Zabbix. Be sure, that all used host names and items are configured. + /// All the items should have type "Zabbix trapper" to support Zabbix sender data flow. + /// + /// Host name or IP address of Zabbix server or proxy. + /// Zabbix server port. + /// Send data request timeout in milliseconds. + /// Stream writer buffer size. + public Sender(string zabbixServer, int port = 10051, int timeout = 500, int bufferSize = 1024) + { + ZabbixServer = zabbixServer; + Port = port; + Timeout = timeout; + BufferSize = bufferSize; + } + + /// + /// Host name or IP address of Zabbix server or proxy. + /// + public string ZabbixServer { get; } + + /// + /// Zabbix server port + /// + public int Port { get; } + + /// + /// Send data request timeout in milliseconds. + /// + public int Timeout { get; } + + /// + /// Stream writer buffer size. + /// + public int BufferSize { get; } + + public Task Send(string host, string key, string value) => + Send(host, key, value, CancellationToken.None); + + public Task Send(string host, string key, string value, CancellationToken cancellationToken) => + Send(new[] + { + new SendData + { + Host = host, + Key = key, + Value = value + } + }, cancellationToken); + + public Task Send(params SendData[] data) => + Send(data, CancellationToken.None); + + public Task Send(IEnumerable data) => + Send(data, CancellationToken.None); + + public async Task Send(IEnumerable data, CancellationToken cancellationToken) + { + using (var tcpClient = new TcpClient()) + { + await tcpClient.ConnectAsync(ZabbixServer, Port); + + using (var networkStream = tcpClient.GetStream()) + { + var serializer = JsonSerializer.Create(new JsonSerializerSettings + { + ContractResolver = new CamelCasePropertyNamesContractResolver() + }); + + using (var ms = new MemoryStream()) + { + using (var writer = new StreamWriter(ms, Encoding.ASCII, BufferSize, true)) + using (var jsonWriter = new JsonTextWriter(writer)) + serializer.Serialize( + jsonWriter, + new + { + request = "sender data", + data, + clock = GetCurrentUnixTime() + }); + + await networkStream.WriteAsync(ZabbixHeader, 0, 5, cancellationToken); + await networkStream.WriteAsync(BitConverter.GetBytes(ms.Length), 0, 8, cancellationToken); + + ms.Seek(0, SeekOrigin.Begin); + await ms.CopyToAsync(networkStream, BufferSize, cancellationToken); + } + + networkStream.Flush(); + + for (int retry = 0; !networkStream.DataAvailable; retry += 50) + { + await Task.Delay(50, cancellationToken); + + if (retry >= Timeout) + throw new TaskCanceledException(); + } + + var response = new byte[BufferSize]; + var count = await networkStream.ReadAsync(response, 0, response.Length, cancellationToken); + var begin = Array.IndexOf(response, (byte)'{'); + + using (var ms = new MemoryStream(response, begin, count - begin)) + { + using (var reader = new StreamReader(ms, Encoding.ASCII)) + using (var jsonReader = new JsonTextReader(reader)) + return serializer.Deserialize(jsonReader); + } + } + } + } + + private static long GetCurrentUnixTime() => + (long)(DateTime.UtcNow - UnixEpoch).TotalSeconds; + } +} diff --git a/ZabbixSender.Async/SenderResponse.cs b/ZabbixSender.Async/SenderResponse.cs new file mode 100644 index 0000000..ac66d2d --- /dev/null +++ b/ZabbixSender.Async/SenderResponse.cs @@ -0,0 +1,9 @@ +namespace ZabbixSender.Async +{ + public class SenderResponse + { + public string Response { get; set; } + public string Info { get; set; } + public bool IsSuccess => Response == "success"; + } +} \ No newline at end of file diff --git a/ZabbixSender.Async/ZabbixSender.Async.csproj b/ZabbixSender.Async/ZabbixSender.Async.csproj new file mode 100644 index 0000000..4311c95 --- /dev/null +++ b/ZabbixSender.Async/ZabbixSender.Async.csproj @@ -0,0 +1,11 @@ + + + + netcoreapp2.1;net461 + + + + + + +