View unanswered posts | View active topics It is currently Tue Dec 23, 2014 1:47 am



Reply to topic  [ 1 post ] 
C source code to change monitor function 
Author Message

Joined: Mon Oct 16, 2006 7:22 pm
Posts: 86
Post C source code to change monitor function
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:
// 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;
}


Thu Jun 07, 2012 7:48 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group