Go to All Forums

Providing input parameters to plugins

It doesn't seem that I can have my plugin (PowerShell) read any files or user-defined environment variables on execution.

I have a plugin that returns stats about a folder. While the plugin is the same on each machine, the folder I want it to monitor differs on each. I tried setting an environment variable for the script to read ($env:<variable>) but it seems unable to do so. I've previously tried to get plugins to read from a configuration file and haven't had any luck there either.

I saw something about allowing a <plugin>.json file and it may be able to use it for some sort of input or configuration but I didn't see any details on how to use it or what was possible there. Is there a way to provide inputs or other machine-specific configuration into a plugin?

Like (4) Reply
Replies (3)

Re: Providing input parameters to plugins

Hi Kevin,

You can use system environment variable inside the script to get the value, but the agent cannot automatically read the user environment variables.

If you want the server agent to read the user environment variables, please follow the steps below:

  1. Site24x7 Windows Agent in services -> Right click -> Open ->Click Log On -> Choose This account and configure username and password. (PFA screenshots )
  2. Once the Log On option for the user is configured, it can access the user environment variables. Else you can configure it as System environment variables. 

<Plugin>.json file can be used to create multiple plugins from a single plugin to change only the input passed to the plugin script. It cannot handle environment variables. But you can tweak your plugin to read the environment variable names from the JSON and use a command to read the environment variable value and process that data.

For eg:

As you have mentioned you want to read some stats from a folder from different machines, you can change your plugin script to get inputs like hostname, username, and password to access, the folder path to monitor from the JSON like below.

{

"localhost":  {
  "host":"localhost",
  "user" : "test" ,
  "password", "***",
  "folderpath" : "ENVRONMENT-VARIABLE-NAME" },

"remotehost":  {
  "host":"remotehostname",
  "user" : "test" ,
  "password", "***",
  "folderpath" : "ENVRONMENT-VARIABLE-NAME" }
}

Inside the powershell script, define the variable used in the json

param([string]$host)
param([string]$user)
param([string]$password)
param([string]$folderpath)


And you can use the variables $host,$user,$password,$env:$folderpath in your plugin script and access according to your requirement.

Let us know if this helps. If you have any queries, please comment in the below thread. 

 

Regards

Anita

Attachments
Like (0) Reply

Re: Re: Providing input parameters to plugins

How can such parameters be passed to a Windows DLL plugin? I couldn't find any information on that. Thanks!

Like (0) Edit Delete Reply

Re: Re: Re: Providing input parameters to plugins

Hi,

There is a way to use multi-configuration with Windows DLL plugins. Create a constructor for the class with parameter names as argument where you created DataCollect method.

Consider the example of a URI response time plugin. The plugin will get the response time of the URI and the http status code, for which we need to give the URI to the plugin.

To monitor multiple URIs with one DLL, create a configuration JSON file.

For example, if the plugin DLL name is ResponseTime.dll, create a ResponseTime.json file with the URIs as given below:

{
"Site24x7_US": {
"uri":"https://status.site24x7.com/"
},
"Site24x7_IN": {
"uri":"https://status.site24x7.in/"
},
"Site24x7_EU": {
"uri":"https://status.site24x7.eu/"
},
}

Inside the plugin DLL, define the constructor for the variables used in the JSON.

class name is ResponseTimeCheck

public ResponseTimeCheck(string uri)

and assign the URI value to a class variable and use that in your plugin code according to your requirement.

DLL plugin sample code with multi-configuration support:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Diagnostics;

namespace ResponseTime
{
   public class ResponseTimeCheck
   {
     private string url;
     private Dictionary<string, object> result = null;
     public string DisplayName
     {
       get{return "ResponseTime_Check";}
     }

     public string Version
     {
       get{return "1";}
     }

     public string Heartbeat
     {
       get{return "True";}
     }

     public object Units()
     {
       Dictionary<String, object> result = new Dictionary<String, object>();
       result.Add("url_response_time", "ms");
       return result;
     }

     public ResponseTimeCheck()
     {
       url = "https://localhost";
     }

     public ResponseTimeCheck(string uri)
     {
       url = uri;
     }

     public void URL_Response_Time(string url)
     {
       ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
       try
       {
         var uri = new Uri(url);
         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
         Stopwatch timer = new Stopwatch();

         timer.Start();

         HttpWebResponse response = (HttpWebResponse)request.GetResponse();
         result.Add("url_status_code", ((int)response.StatusCode).ToString());
         if ((int)response.StatusCode >= 400)
         {
           result.Add("status", 0);
         }
         response.Close();

         timer.Stop();

         TimeSpan timeTaken = timer.Elapsed;
         result.Add("url_response_time", timeTaken.Milliseconds.ToString());
       }
       catch (Exception e)
       {
         result.Add("url_response_time", "0");
         result.Add("url_status_code", "0x"+e.HResult.ToString("X"));
         result.Add("msg", e.Message);
         result.Add("status", 0);
       }
     }
     public object DataCollect()
     {
       result = new Dictionary<String, object>();
       URL_Response_Time(this.url);
       result.Add("name",this.nameval);
       return result;
     }
   }
}

Refer the above sample code for your plugin implementation with multi-configuration. Let us know if this helps. If you have any queries, please comment in the below thread. 

 

Regards,

Hisham Thorakkal

Like (0) Reply

Was this post helpful?