C source code to change monitor function

If you've made a patch to quick fix a bug or to add a new feature not yet in the main tree then post it here so others can try it out.
Post Reply
BobS0327
Posts: 86
Joined: Mon Oct 16, 2006 6:22 pm

C source code to change monitor function

Post by BobS0327 »

Listed below is the C source code to change the function of a particular monitor. It has been compiled and tested on Ubuntu Linux server.

Code: Select all

// Software is distributed 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.

#include<stdlib.h>
#include<stdio.h>
#include <string.h>
#include <my_global.h>
#include <mysql.h>

struct list_monitor_names {
    char  monitor_name[65];
    struct list_monitor_names * next;
};

typedef struct list_monitor_names monitor_record;


int mysql_exec_sql(MYSQL *mysql,const char *create_definition)
{
    return  mysql_real_query(mysql,create_definition,strlen(create_definition));
}

int main(int argc, char **argv)
{
    char *functions[6] =  { "None", "Monitor", "Modect", "Record", "Mocord", "Nodect"};
    monitor_record *curr, *head;
    MYSQL conn;
    MYSQL_RES *res;
    MYSQL_ROW row;
    char *server = "localhost";
    char *database = "zm";
    int len;
    int x;
    int validfunction = 0;
    int validmonitor = 0;
    char ch;
    head = NULL;
    char record[1000] = {0};

    if(argc != 5)
    {
        printf("Invalid command line arguments: Usage %s userid password monitorname updatedfunction\n", argv[0]);
        return -1;
    }

    /* Validate monitor function */
    for(x = 0; x < 6; x++)
    {
        if(strcmp(argv[4], functions[x]) == 0)
        {
            validfunction = 1;
            break;
        }
    }

    if(!validfunction)
    {
        printf("Aborting, %s is NOT a valid monitor function.  Please be aware that function is CASE SENSITIVE\n\n", argv[4]);
        return -1;
    }

    mysql_init(&conn);

    /* Connect to database */
    if (!mysql_real_connect(&conn, server, argv[1], argv[2], database, 0, NULL, 0))
    {
        fprintf(stderr, "%s\n", mysql_error(&conn));
        return 1;
    }

    /* send SQL query */
    if (mysql_query(&conn, "SELECT Name FROM Monitors"))
    {
        fprintf(stderr, "%s\n", mysql_error(&conn));
        return 1;
    }

    res = mysql_use_result(&conn);

    /* Load monitor names into linked list */
    while ((row = mysql_fetch_row(res)) != NULL)
    {
        if (NULL == (curr = malloc(sizeof(monitor_record))))
        {
            printf("malloc failed, aborting\n");
            return 1;
        }
        strcpy(curr->monitor_name, row[0]);
        curr->next  = head;
        head = curr;
    }

    curr = head;
    /* Verify input monitor name is found in zm database */
    while(curr) {
        if(strcmp(argv[3], curr->monitor_name) == 0)
        {
            validmonitor = 1;
            break;
        }
        curr = curr->next ;
    }

    /* Update record */
    if(validmonitor)
    {
        if(mysql_select_db(&conn,"zm")==0)/*success*/
            printf( "Database Selected\n");
        else
            printf( "Failed to connect to Database: Error: %s\n",mysql_error(&conn));
        sprintf(record,"UPDATE Monitors Set Function='%s' WHERE Name='%s'",argv[4], argv[3]);
        if(mysql_exec_sql(&conn,record)==0)/*success*/
            printf( "Record Updated\n");
        else
            printf( "Failed to update record: Error: %s\n",
                    mysql_error(&conn));
    }
    else printf("%s was not found in ZM database, could not update record\n", argv[3]);

    /* close connection */
    mysql_free_result(res);
    mysql_close(&conn);
	return 0;
}
Post Reply