Click here to Skip to main content
15,891,423 members
Articles / Programming Languages / C#
Tip/Trick

Connect to observu.com monitor and send data

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
24 Jul 2012CPOL 9.2K   42   2  
Observu monitors your websites from multiple locations and combines it with measurements collected on your servers. Here this article will show how to send data to Observu.

This article appears in the Third Party Products and Tools section. Articles in this section are for the members only and must not be used to promote or advertise products in any way, shape or form. Please report any spam or advertising.

Introduction

Using this DLL one can easily send data to observu monitor. To connect with observu monitor you need a JSON encoded set of records. Using this DLL you can send data easily by submitting the parameter. So basically you don't need to worry about JSON encoded set of records.

Background

a JSON encoded set of records that need to be added to the system. The records variable should look something like:

[
   {"timestamp": 1234568, /* unix timestamp of the event */
    "monitor_id":24324, /* only required if not implied by the API key */
    "monitor_name":"my host", /* only required if not implied by the API key
                                and monitor_id is unknown, this requires the API
                                key to specify at least the service/group level
                                */
    "monitor_tags":{  /* not required */
      "role": "web"
    },
    "data": { /* required */
       'load:float': 4.52,    
       'disks.dev_sda.available:bytes': 523453, 
       'apache_running:boolean': true, 
       /*........ more vars */
    },
    /*................ more records .......... */
]

To send data we need to send a HTTP POST to: https://observu.com/api/add and the input parameter key (Optional string):
an access key obtained from the observu website source (Optional string)
a string that (uniquely) describes the current monitor with this dll you can send sigle data,multiple data and can obtain new key and monitor id

Using the Code

Use of this DLL is simple. Just add the DLL to the project see here

//
//   string[] name = new string[1];
//   string[] type = new string[1];
//   Int64[] value = new Int64[1];
//   name[0] = "em.o";
//   type[0] = "int";
//   value[0] = 85;
//   name[1] = "em.t";
//   type[1] = "int";
//   value[1] = 85;
//   ConnectToObservu testConnect = new ConnectToObservu();
//   if (testConnect.sendSingleData(name[0], type[0], value[0], "qUXeT0piB0M4rT1EGOt", "843"))
//        Console.WriteLine("server message="+testConnect.responseFromServer);

//

...

How the DLL Created

Get a key automatically

//
public string[] addMonitorandGetKeyAndID(string username, string password, string servicename, string monitorname)
        {
            WebRequest request = WebRequest.Create("https://observu.com/api/add-external-monitor");
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            string postData = "username=" + username + "&password=" + password + "&service_name=" + servicename + "&monitor_name=" + monitorname + "";

            
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            System.IO.Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            System.IO.StreamReader reader = new System.IO.StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            // MessageBox.Show(responseFromServer);
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();

            string[] r = responseFromServer.Split(',');
            r = r[0].Split(':');
            Console.WriteLine(r[1]);
            string[] sendValue = new string[2];
            if (r[1] != "\"ok\"")
            {
                string[] words = responseFromServer.Split(',');
                //get monitor id
                string[] monitor_id = words[2].Split(':');
                monitor_id = monitor_id[1].Split('\"');

                //get key
                words = words[1].Split(':');
                words = words[1].Split('\"');


                

                sendValue[0] = words[1];
                sendValue[1] = monitor_id[1];

                return sendValue;
            }

            return sendValue;
        }
...

Send data to the observu server

public bool sendSingleData(string name,string type,Int64 value,string key,string monitorid)
        {
            string responseFromServer=null;
            try
            {

                // Create a request using a URL that can receive a post. 
                WebRequest request = WebRequest.Create(" https://observu.com/api/add");
                // Set the Method property of the request to POST.
                request.Method = "POST";
                // Create POST data and convert it to a byte array.

               Int64 timestamp = UnixTimestamp();
                
                string  datum = "\"data\":{\""+name+":"+type+"\""+":"+value.ToString()+"}";
                
                //MessageBox.Show(datum);
                string postData = "key=" + key + "&records=[{\"timestamp\":" + timestamp.ToString() + ", \"monitor_id\":" + monitorid + ", " + datum + "}]";
                
                byte[] byteArray = Encoding.UTF8.GetBytes(postData);
                // Set the ContentType property of the WebRequest.
                request.ContentType = "application/x-www-form-urlencoded";
                // Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length;
                // Get the request stream.
                Stream dataStream = request.GetRequestStream();
                // Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.
                dataStream.Close();
                // Get the response.
                WebResponse response = request.GetResponse();
                // Display the status.
                //MessageBox.Show(((HttpWebResponse)response).StatusDescription);
                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.
                responseFromServer = reader.ReadToEnd();
                // Display the content.
                //MessageBox.Show(responseFromServer);
                // Clean up the streams.
                reader.Close();
                dataStream.Close();
                response.Close();
            }
            catch (Exception e)
            {
                return false;
            }
            string[] r = responseFromServer.Split(',');
                r = r[0].Split(':');
                if (r[1] == "\"ok\"")
                    return true;
            return false;
        }

Reference

  • https://observu.com/docs/api
  • https://observu.com/docs/start

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior)
Bahamas Bahamas
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --