📜 ⬆️ ⬇️

We deal with Zabbix API in C #

Zabbiks has an open API for accessing monitoring server functions. Unfortunately, the implementation of the library for accessing the API exists only for Ruby, PHP and Python. I had to reinvent the wheel by myself. Access to all functions is via JSONRPC . For example, the request, the response of the function to get the Zabbix API version looks like this:
{ "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 .


A detailed description of the protocol JSON-RPC is not included in the purpose of the article, proceed to the implementation:
  1. private string GetWebRequest( string body)
  2. {
  3. WebRequest wb = WebRequest.Create(url);
  4. wb.ContentType = @"application/json-rpc" ;
  5. wb.Credentials = CredentialCache.DefaultCredentials;
  6. ASCIIEncoding encoding = new ASCIIEncoding();
  7. string postData = body;
  8. byte [] data = encoding.GetBytes(postData);
  9. wb.Method = "POST" ;
  10. wb.ContentLength = data.Length;
  11. Stream newStream = wb.GetRequestStream();
  12. newStream.Write(data, 0, data.Length);
  13. newStream.Close();
  14. HttpWebResponse response = (HttpWebResponse)wb.GetResponse();
  15. Stream dataStream = response.GetResponseStream();
  16. StreamReader reader = new StreamReader(dataStream);
  17. return reader.ReadToEnd();
  18. }
  19. public string obj2json( object obj)
  20. {
  21. JavaScriptSerializer serializer = new JavaScriptSerializer();
  22. return serializer.Serialize(obj);
  23. }
  24. public string CallApi( string method, object param)
  25. {
  26. object Query = new
  27. {
  28. jsonrpc = "2.0" ,
  29. auth = auth_hash,
  30. id = id.ToString(),
  31. method = method,
  32. Params = param
  33. };
  34. String qr = obj2json(Query);
  35. qr=qr.Replace( "Params" , "params" );
  36. id++;
  37. string result = GetWebRequest(qr);
  38. return result;
  39. }

* This source code was highlighted with Source Code Highlighter .

The CallAPI function makes an API call to the function. Accepts a method and its parameters (in the form of an object according to the documentation of the Zabbix API). Examples of use will be slightly lower. The purpose of obj2json , and GetWebRequest I think obviously, I will add only that the request is sent by the POST method. Consider the use of this function on the example of authorization on the server. For authorization, we need to send a request for the “user.authenticate” method with user and password parameters. The result will be auth_hash, which is the field of each subsequent query. The finished function for authorization with the foregoing will look like this:
  1. public bool login()
  2. {
  3. bool res;
  4. Update( new UpdateInfoMessage( this ) { message = " " , status = "LOGIN" });
  5. try
  6. {
  7. var userinfo = new { user = _user, password = _password };
  8. string result = CallApi( "user.authenticate" , userinfo);
  9. auth_hash = (serializer.Deserialize< string >(result)).result;
  10. res= true ;
  11. Update( new UpdateInfoMessage( this ) { message = " " , status = "LOGIN" });
  12. }
  13. catch (Exception ex)
  14. {
  15. Update( new UpdateInfoMessage( this ) { message = " :" + ex.Message, status = "LOGIN" });
  16. res= false ;
  17. }
  18. return res;
  19. }

* This source code was highlighted with Source Code Highlighter .

As you can see, the logic to the disgrace is simple, we describe the parameters, set the name of the method, call the function. If everything went well and there is an auth_hash , then we save it in the global class field and return a positive result. If something went wrong, we catch the error and return a lie. It is also worth mentioning the Update function - it is used to notify the main application about changes in our class. So, we learned to fulfill the request and receive simple answers from the server. But most of the responses from the server are arrays of completely different elements. To simplify the addition of a new functional, the following generalized class was created:
  1. 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 , . :
    1. using System;
    2. using System.Collections. Generic ;
    3. using System.Collections.ObjectModel;
    4. using System.Linq;
    5. using System.Text;
    6.  
    7. namespace Zabbix
    8. {
    9. 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