{ "jsonrpc" : "2.0" , "method" : "apiinfo.version" , "params" :[], "auth" : "a6e895b98fde40f4f7badf112fd983bf" , "id" :2 } <br>{ "jsonrpc" : "2.0" , "result" : "1.3" , "id" :2 } * This source code was highlighted with Source Code Highlighter .
private string GetWebRequest( string body)
{
WebRequest wb = WebRequest.Create(url);
wb.ContentType = @"application/json-rpc" ;
wb.Credentials = CredentialCache.DefaultCredentials;
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = body;
byte [] data = encoding.GetBytes(postData);
wb.Method = "POST" ;
wb.ContentLength = data.Length;
Stream newStream = wb.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse response = (HttpWebResponse)wb.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
return reader.ReadToEnd();
}
public string obj2json( object obj)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(obj);
}
public string CallApi( string method, object param)
{
object Query = new
{
jsonrpc = "2.0" ,
auth = auth_hash,
id = id.ToString(),
method = method,
Params = param
};
String qr = obj2json(Query);
qr=qr.Replace( "Params" , "params" );
id++;
string result = GetWebRequest(qr);
return result;
}
* This source code was highlighted with Source Code Highlighter .
public bool login()
{
bool res;
Update( new UpdateInfoMessage( this ) { message = " " , status = "LOGIN" });
try
{
var userinfo = new { user = _user, password = _password };
string result = CallApi( "user.authenticate" , userinfo);
auth_hash = (serializer.Deserialize< string >(result)).result;
res= true ;
Update( new UpdateInfoMessage( this ) { message = " " , status = "LOGIN" });
}
catch (Exception ex)
{
Update( new UpdateInfoMessage( this ) { message = " :" + ex.Message, status = "LOGIN" });
res= false ;
}
return res;
}
* This source code was highlighted with Source Code Highlighter .
public class Result: IEnumerable
{//
public T[] result;
///
public ZabbixConnection server;
// Zabbix API, maps.get, triggers.update .
protected string method;
//
protected object Params;
// ,
public string stringResult;
// .
public object SyncRoot;
// , "method" "Params"
protected virtual void init() { }
public Result(ZabbixConnection Server)
{
init();
server = Server;
SyncRoot = new object ();
}
public Result()
{
init();
}
// , result collection
public virtual void get ()
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
lock (SyncRoot)
{
stringResult = (server.CallApi(method, Params));
result = serializer.Deserialize<Result>(stringResult).result;
if (result== null ){result= new T[1];}
}
}
public T this [ int index]
{
get
{
return result[index];
}
}
public IEnumerator GetEnumerator()
{
foreach (T item in result)
{
yield return item;
}
}
IEnumerator IEnumerable .GetEnumerator()
{
return GetEnumerator();
}
}
, Zabbix API . , Init , . :
using System;
using System.Collections. Generic ;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
namespace Zabbix
{
public class Hosts:Result
{
protected override void init()
{
method = "host.get" ;
Params = new { output = "extend" };
}
public Hosts(ZabbixConnection Server) : base (Server) { }
}
public class Host
{
public string host;
public string hostid;
public string ip;
public override string ToString()
{
return host;
}
}
}
I think everything here is also quite simple, in the same way you can get any data, be it triggers, host groups, events, or graph data. Descriptions of all methods and parameters can be found in the documentation section of the Zabbix API . I feel the topic and so it turned out not small, and it's time to wrap up. I described the way to use Zabbix API in .NET applications as short as possible. A more complete library code, as well as a sample client on WPF, can be downloaded here: https://github.com/p1nger/odzl
Source: https://habr.com/ru/post/111641/
All Articles