Skip to content

Commit

Permalink
Protocol fix. Updated README.
Browse files Browse the repository at this point in the history
  • Loading branch information
stop-cran committed Dec 3, 2018
1 parent 82457f5 commit c5f522f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 45 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Synopsis
# Overview

The package provides a tool to send data to Zabbix in the same way as [zabbix_sender](https://www.zabbix.com/documentation/4.0/ru/manual/concepts/sender) tool. It implements [Zabbix Sender Protocol 4.0](https://www.zabbix.org/wiki/Docs/protocols/zabbix_sender/4.0).

Expand All @@ -14,7 +14,11 @@ PM> Install-Package ZabbixSender.Async

```C#
var sender = new ZabbixSender.Async.Sender("192.168.0.10");
var response = sender.Send("MonitoredHost1", "trapper.item1", "10");
var response = sender.Send("MonitoredHost1", "trapper.item1", "12");
Console.WriteLine(reponse.Response); // "success" or "fail"
Console.WriteLine(response.Info); // e.g. "Processed 1 Failed 0 Total 1 Seconds spent 0.000253"
```
```

# Remarks

Note, that in order for the request to be accepted, hosts like `MonitoredHost1` above, should be [configured](https://www.zabbix.com/documentation/4.0/manual/config/hosts/host). The same should do the items (like `trapper.item1` above). The item type should have [Zabbix trapper](https://www.zabbix.com/documentation/4.0/manual/config/items/itemtypes/trapper). Also the values passed (`12` above) should respect the type of information, configured for each item.
53 changes: 25 additions & 28 deletions ZabbixSender.Async/Formatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -104,48 +105,44 @@ await stream.WriteAsync(BitConverter.GetBytes(ms.Length), 0,

public SenderResponse ReadResponse(Stream stream)
{
int responseSize = Math.Max(bufferSize, 128);
var response = new byte[responseSize];
var count = stream.Read(response, 0, response.Length);
var begin = Array.IndexOf(response, (byte)'{');

if (count <= 0)
throw new ProtocolException("empty reponse received");
try
{
var buffer = new byte[13];

if (begin == -1)
throw new ProtocolException("start of Json ({) not found", response);
stream.Read(buffer, 0, buffer.Length); // skip the length

if (count >= responseSize)
throw new ProtocolException("the response is too big", response);
if (ZabbixHeader.Zip(buffer, (x, y) => x != y).Any(b => b))
throw new ProtocolException("the response has an incorrect header");

try
{
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<SenderResponse>(jsonReader);
}
using (var reader = new StreamReader(stream, Encoding.ASCII))
using (var jsonReader = new JsonTextReader(reader))
return serializer.Deserialize<SenderResponse>(jsonReader);
}
catch (JsonException ex)
{
throw new ProtocolException("invalid response format", ex, response);
throw new ProtocolException("invalid response format", ex);
}
}

public async Task<SenderResponse> ReadResponseAsync(Stream stream,
CancellationToken cancellationToken)
public async Task<SenderResponse> ReadResponseAsync(Stream stream, CancellationToken cancellationToken)
{
var response = new byte[1024];
var count = await stream.ReadAsync(response, 0, response.Length, cancellationToken);
var begin = Array.IndexOf(response, (byte)'{');

using (var ms = new MemoryStream(response, begin, count - begin))
try
{
using (var reader = new StreamReader(ms, Encoding.ASCII))
var buffer = new byte[13];

await stream.ReadAsync(buffer, 0, buffer.Length, cancellationToken); // skip the length

if (ZabbixHeader.Zip(buffer, (x, y) => x != y).Any(b => b))
throw new ProtocolException("the response has an incorrect header");

using (var reader = new StreamReader(stream, Encoding.ASCII))
using (var jsonReader = new JsonTextReader(reader))
return serializer.Deserialize<SenderResponse>(jsonReader);
}
catch (JsonException ex)
{
throw new ProtocolException("invalid response format", ex);
}
}
}
}
12 changes: 0 additions & 12 deletions ZabbixSender.Async/ProtocolException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,5 @@ public ProtocolException(string message) :
public ProtocolException(string message, Exception innerException) :
base($"Protocol error - {message}.", innerException)
{ }

public ProtocolException(string message, byte[] response) :
base($"Protocol error - {message}. See the whole response in Data property by Response key.")
{
Data.Add("Response", response);
}

public ProtocolException(string message, Exception innerException, byte[] response) :
base($"Protocol error - {message}. See the whole response in Data property by Response key.", innerException)
{
Data.Add("Response", response);
}
}
}
4 changes: 2 additions & 2 deletions ZabbixSender.Async/ZabbixSender.Async.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;net461</TargetFrameworks>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Copyright>Apache License 2.0 (stop-cran, 2018)</Copyright>
<Authors>stop-cran &lt;[email protected]&gt;</Authors>
<Company>stop-cran &lt;[email protected]&gt;</Company>
<RepositoryUrl>https://github.com/stop-cran/ZabbixSender.Async</RepositoryUrl>
<PackageReleaseNotes>Added clock. Added interfaces for a possibility of DI.</PackageReleaseNotes>
<PackageReleaseNotes>Protocol fix.</PackageReleaseNotes>
<Description>The package provides a tool to send data to Zabbix in the same way as zabbix_sender tool. It implements Zabbix Sender Protocol 4.0.</Description>
</PropertyGroup>

Expand Down

0 comments on commit c5f522f

Please sign in to comment.