/*****************************************************************************
*
* Check_openflow plugin
* Ver 0.1
*
* License: GPLv2
* Copyright (c) 2011 TSC OSS Development Team
*
* Description:
*
* This file contains the check_openflow plugin
*
* This plugin checks the state, description, flow-table information of
* specified switch with openflow protocol support.
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*
* complie:
* gcc check_openflow.c -o check_openflow
*
* Usage:
*
* ./check_openflow -q ""
*
* 1. check openvswitch uptime
* e.g local server: ./check_openflow t -h 'tcp:127.0.0.1:6634'
* e.g remote server: ./check_by_ssh -H 192.168.56.101 -l openflow -C "/home/openflow/wa3/check_openflow t -h 'tcp:127.0.0.1:6634'"
*
* 2. check openvswitch description
* e.g local server: ./check_openflow d -h 'tcp:127.0.0.1:6634'
*
* 3. check openvswitch link status
* e.g local server: ./check_openflow e -h 'tcp:127.0.0.1:6634' -n'eth0'
*
* 4. check openvswitch port information
* e.g local server: ./check_openflow p -h 'tcp:127.0.0.1:6634' -o'1' //1=port 1;2=port 2
*
* 5. check openvswitch flow-table information
* e.g local server: ./check_openflow w -h 'tcp:127.0.0.1:6634' -o'1' //aduiting all snmpdriver
*****************************************************************************/
const char *progname = "check_openflow";
const char *copyright = "2011";
const char *email = "oss.shoei@gmail.com";
#include
#include
#include
#include
#include
#define BUF 256
char *command=NULL;
char *host=NULL;
char *nic=NULL;
char *port=NULL;
char *flow=NULL;
int parse_params_long(int ac, char **av);
int switch_desc();
int switch_uptime();
int switch_linkstatus();
int switch_traffic();
int switch_flowtable();
static int verbose_flag;
int main (int argc, char **argv)
{
//parse_params_long(argc, argv);
if(argv[1]==NULL){
exit(1);
}
//printf("argv[1]: %s\n",argv[1]);
switch(argv[1][0]){
case 'l': //check openswitch link status
{
parse_params_long(argc, argv);
FILE *fp;
char buf[BUF];
//char *cmdline = "/bin/ls /bin";
char *cmdline = command;
if ( (fp=popen(cmdline,"r")) ==NULL) {
err(EXIT_FAILURE, "%s\n", cmdline);
}
while(fgets(buf, BUF, fp) != NULL) {
(void) fputs(buf, stdout);
}
(void) pclose(fp);
exit (EXIT_SUCCESS);
}
case 't': //check openswitch uptime
{
parse_params_long(argc, argv);
switch_uptime();
}
case 'e': //check openswitch linkstatus
{
parse_params_long(argc, argv);
switch_linkstatus();
}
case 'd': //check openswitch description
{
parse_params_long(argc, argv);
switch_desc();
}
case 'p': //check openswitch port information
{
parse_params_long(argc, argv);
//printf("start P!!\n");
switch_traffic();
}
case 'f': //check openswitch flow-table information
{
parse_params_long(argc, argv);
//printf("start P!!\n");
switch_flowtable();
}
default: //parent
{
sleep(1);
break;
}
}
}
/*
process command-line long arguments
*/
int parse_params_long (int ac,char **av)
{
int c;
while (1)
{
static struct option long_options[] =
{
/* These options set a flag. */
{"verbose", no_argument, &verbose_flag, 1},
{"brief", no_argument, &verbose_flag, 0},
/* These options don't set a flag.
We distinguish them by their indices. */
//{"commandquery", required_argument, 0, 'q'},
{"host", required_argument, 0, 'h'},
{"cmdquery", optional_argument, 0, 'q'},
{"nic", optional_argument, 0, 'n'},
{"port", optional_argument, 0, 'o'},
{"flowtable", optional_argument, 0, 'w'},
//{"dodaemon", required_argument, 0, 'd'},
{0, 0, 0, 0}
};
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long (ac, av, "h:q::n::o::w::",
long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
switch (c)
{
case 0:
/* If this option set a flag, do nothing else now. */
if (long_options[option_index].flag != 0)
break;
printf ("option %s", long_options[option_index].name);
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
break;
case 'q':
//puts ("option -u\n");
//printf ("option -q with value `%s'\n", optarg);
command=optarg;
break;
case 'h':
host=optarg;
break;
case 'n':
nic=optarg;
break;
case 'o':
port=optarg;
//printf ("option -o with value `%s'\n", optarg);
break;
case 'w':
flow=optarg;
//printf ("option -o with value `%s'\n", optarg);
break;
case '?':
/* getopt_long already printed an error message. */
break;
default:
abort ();
}
}
if (verbose_flag)
puts ("verbose flag is set");
/* Print any remaining command line arguments (not options). */
/*
if (optind < ac)
{
printf ("non-option ARGV-elements: ");
while (optind < ac)
printf ("%s ", av[optind++]);
putchar ('\n');
}
*/
//exit (0);
}
int switch_desc(){
//parse_params_long(argc, argv);
//printf("Start check openswitch uptime...\n");
FILE *fp;
char buf[BUF];
//char *cmdline = "/bin/ls /bin";
char cmdline[] = "ovs-ofctl dump-desc ";
char cmdoption[] = " | egrep 'Hardware|Software|Serial'";
//char tmpbuf[BUF];
char *buf1;
strcat(cmdline,host);
strcat(cmdline,cmdoption);
//printf("%s",cmdline);
if ( (fp=popen(cmdline,"r")) ==NULL) {
err(EXIT_FAILURE, "%s", cmdline);
return 3;
}
while(fgets(buf, BUF, fp) != NULL) {
//(void) fputs(buf, stdout);
buf1=strtok(buf,"\n");//printf("buf1: %s",buf1);
strcat(buf1,";");//printf("buf1: %s",buf1);
(void) fputs(buf1, stdout);
//strcat(tmpbuf,buf1);printf("tmpbuf: %s",buf1);
}
(void) pclose(fp);
if(strlen(buf)<=2){
exit(0);
}
else{
exit(1);
}
//(void) pclose(fp);
//exit (EXIT_SUCCESS);
}
int switch_uptime(){
FILE *fp;
char buf[BUF];
//char *cmdline = "/bin/ls /bin";
char cmdline[] = "ovs-ofctl status ";
char cmdoption[] = " | egrep -a 'uptime'";
strcat(cmdline,host);
strcat(cmdline,cmdoption);
//printf("%s",cmdline);
if ( (fp=popen(cmdline,"r")) ==NULL) {
err(EXIT_FAILURE, "%s", cmdline);
}
while(fgets(buf, BUF, fp) != NULL) {
(void) fputs(buf, stdout);
}
(void) pclose(fp);
if(strlen(buf)<=2){
exit(0);
}
else{
exit(1);
}
//(void) pclose(fp);
//exit (EXIT_SUCCESS);
}
int switch_linkstatus(){
FILE *fp;
char buf[BUF];
//char *cmdline = "/bin/ls /bin";
char cmdline[] = "ovs-ofctl show ";
char cmdoption[] = " | egrep '0x1' | egrep '";
char cmdoption1[] = "'";
strcat(cmdline,host);
strcat(cmdline,cmdoption);
strcat(cmdline,nic);
strcat(cmdline,cmdoption1);
if ( (fp=popen(cmdline,"r")) ==NULL) {
err(EXIT_FAILURE, "%s", cmdline);
}
while(fgets(buf, BUF, fp) != NULL) {
(void) fputs(buf, stdout);
}
(void) pclose(fp);
if(strlen(buf)<=2){
exit(0);
}
else{
exit(1);
}
}
int switch_traffic(){
//printf("start process traffic.\n");
FILE *fp;
char buf[BUF];
//char *cmdline = "/bin/ls /bin";
char cmdline[BUF] = "dpctl dump-ports ";
char cmdoption[] = " | egrep 'rx|tx'";
char cmdspace[] = " ";
char *buf1;
strcat(cmdline,host);
strcat(cmdline,cmdspace);
strcat(cmdline,port);
strcat(cmdline,cmdoption);
//printf("%s",cmdline);
if ( (fp=popen(cmdline,"r")) ==NULL) {
err(EXIT_FAILURE, "%s", cmdline);
}
while(fgets(buf, BUF, fp) != NULL) {
//todo: process string tx and rx.
//process rx data
char *sp;
sp = strstr(buf,"rx");
if(sp!=NULL){
buf1=strtok(buf,"\n");
strcat(buf1,";");
(void) fputs(buf1, stdout);
}
else{
sp = strstr(buf,"tx");
(void) fputs(sp, stdout);
}
}
(void) pclose(fp);
if(strlen(buf)<=2){
exit(3);
}
else{
exit(0);
}
}
int switch_flowtable(){
//parse_params_long(argc, argv);
//printf("Start check openswitch uptime...\n");
FILE *fp;
char buf[BUF];
//char *cmdline = "/bin/ls /bin";
char cmdline[] = "dpctl dump-flows ";
char cmdoption[] = " | egrep 'actions'";
//char tmpbuf[BUF];
char *buf1;
strcat(cmdline,host);
strcat(cmdline,cmdoption);
//printf("%s",cmdline);
if ( (fp=popen(cmdline,"r")) ==NULL) {
err(EXIT_FAILURE, "%s", cmdline);
return 3;
}
while(fgets(buf, BUF, fp) != NULL) {
//(void) fputs(buf, stdout);
buf1=strtok(buf,"\n");//printf("buf1: %s",buf1);
strcat(buf1,";");//printf("buf1: %s",buf1);
(void) fputs(buf1, stdout);
//strcat(tmpbuf,buf1);printf("tmpbuf: %s",buf1);
}
(void) pclose(fp);
if(strlen(buf)<=2){
exit(0);
}
else{
exit(1);
}
//(void) pclose(fp);
//exit (EXIT_SUCCESS);
}