Page 1 of 1

Custem Button on the WebUI

Posted: Thu May 21, 2015 4:27 am
by Mapienator
I am trying to get a button in nagios to enable/disable (a toggle) in the web-UI
Also a way to print the output of the command {service ssh status}

I am able to do this with linking to a php page that has a simple perl code. What I want is the following code in a Nagios page (.cgi file).

Code: Select all

<?php
if ($_GET['run']) {
  # This code will run if ?run=true is set.
  exec("sudo scripts/ssh-start");
}
if ($_GET['runf']) {
  # This code will run if ?run=true is set.
  exec("sudo scripts/ssh-stop");
}

$status = exec('sudo scripts/ssh-status');

$check = substr($status, 0, 8);

if ($check == "ssh stop"){
	echo "<a href='?run=true'>Click Me! - ssh start</a>";	
}

else{
	echo "<a href='?runf=true'><img src='action.gif'></img></a>";		
}
?>
But i'm having trouble with converting it. Does anyone have something like this?

Re: Custem Button on the WebUI

Posted: Thu May 21, 2015 9:16 am
by tmcdonald
This would need to be written in C if you want it as a .cgi file. Is there something about PHP you don't like or do you just prefer it to be a CGI?

Re: Custem Button on the WebUI

Posted: Fri May 22, 2015 3:34 am
by Mapienator
The main problem now is that i want to display a bash command output in a page that is made in c (extinfo.cgi). and i don't want any popups.

The code below is what I'm working on now. But is has problems.....

Code: Select all

char i;
  printf ("Checking if processor is available...");
  if (system(NULL)){ printf ("Ok");}
    else {
		exit (EXIT_FAILURE);
	printf("not ok");
	}
  printf ("Executing command ls...<br>\n");
//bash command: env
  i = system ('env');
  printf ("<br>The value returned was: %s.\n",i);

Code: Select all

Output:
Checking if processor is available...OkExecuting command ls...The value returned was: (null).
And I am not that skilled in programming, that's probaly the reason why I cant this to work.

Re: Custem Button on the WebUI

Posted: Fri May 22, 2015 9:11 am
by tmcdonald
The "system()" function in C will return the exit status of whatever you run, not the output. If you want to capture the output, check this guide out:

http://stackoverflow.com/questions/6462 ... get-output

Also keep in mind, while some of us on this forum are proficient in C, this is a Nagios forum. If you need assistance with the actual C language you should ask on a C forum.

Re: Custem Button on the WebUI

Posted: Fri May 29, 2015 2:47 am
by Mapienator
Well. Thanks everyone for helping. I got it working. Maybe someone else can use it too.
I use this in my "extinfo.cgi" on the page "Process Info"

Code: Select all

printf("<tr><td>SSH Status</td>\n");
	/* SSH Status */
	#include <stdio.h>
#include <stdlib.h>
#include <string.h>

  FILE *fp;
  char path[1035];

  /* Open the command for reading. */
  fp = popen("sudo ssh-status", "r");
  if (fp == NULL) {
    printf("Failed to run command\n" );
    exit(1);
  }

  /* Read the output a line at a time - output it. */
  while (fgets(path, sizeof(path)-1, fp) != NULL) {
	  char *output[10];
	  char *control_status = "";
	  char *control_string = "";	  
	  int ret;
	  
	  control_status = path;	  
	  control_string = "ssh sta";
	  

	strncpy(output, control_status, 7);
	output[3] = '\0';

	ret = strcmp(output, control_string);
	if(ret == 0){
		printf("<td><div style='background-color: #00cc33; color:#fff; text-align:center;'>Enabled</div>\n");
	}
	else{
		printf("<td><div style='background-color: #ff0000; color:#fff; text-align:center;'>Disabled</div>\n");
	}
	
  }

  /* close */
  pclose(fp);
  
  printf("</td></tr>\n");

Re: Custem Button on the WebUI

Posted: Fri May 29, 2015 9:10 am
by tmcdonald
You are quite welcome!

I'll be closing this thread now, but feel free to open another if you need anything in the future!