/*
Program :InnoDB_checker.c
Description:Write a C/C++ program that connects to a MySQL server and checks if the InnoDB plug-in is installed on it.
If so, your program should print the total number of disk writes by MySQL.
Date :7/09/2011
Version :1.0
Author :Venugopal Madathil*/
/*Command to run the program: gcc -o output -I/usr/include/mysql/ -lmysqlclient InnoDB_checker.c -w */
#include<stdio.h>
#include<mysql.h>
#include<string.h>
int main(int argc,char *argv[])
{
MYSQL *connection;
MYSQL_RES *result;
MYSQL_ROW *row;
//printf("\nI'm here\n");//Debugger
char *server = "localhost";//Since system itself acts as the server
//Credentials initialization
char *user = "root";
char *password = "pass";//Must
char *database = "webyog";//Must be an existing database name
int numfields,Inno_flag=0,CSV_flag=0,c;
char choice;
do
{
system("clear");//To clear the terminal screen
printf("----------------------\nPlug-in checker\n");
printf("----------------------\n");
connection = mysql_init(NULL);//Allocates or initializes a MYSQL object suitable for mysql_real_connect,if NULL it returs a new object
if(!mysql_real_connect(connection,server,user,password,database,0,NULL,0))//Connection establishment
{
printf("\nConnection error:");
fprintf(stderr,"%s\n",mysql_error(connection));//Error
}
if(mysql_query(connection,"show engines"))
{
printf("\nQuery execution failed:");
fprintf(stderr,"%s\n",mysql_error(connection));//Error
}
result = mysql_use_result(connection);//Fetching result
numfields = mysql_num_fields(result);
//printf("\nNumber of fields:%d\n",numfields);//Number of fields
printf("What do you want to check?\nPress 1 for InnoDB\nPress 2 for CSV support\nYour choice: ");
scanf("%d",&c);
//
while((row = mysql_fetch_row(result)) != NULL)//Iterating result row
{
if((strcmp(row[0], "InnoDB") == 0) && strcmp(row[1], "YES") == 0)//Innodb plug in checking
Inno_flag=1;
if((strcmp(row[0], "CSV") == 0) && strcmp(row[1], "YES") == 0)//CSV support checking
CSV_flag=1;
}
switch(c)
{
case 1:
if(!Inno_flag)
printf("InnoDB plug-in is not installed\n");
else
printf("InnoDB plug-in is installed\n");
mysql_free_result(result);//Freeing the buffer
//To fetch Total Disk writes by MySQL
if(mysql_query(connection, "show global status"))
{
printf("\nQuery execution failed:");
fprintf(connection, "%s\n", mysql_error(connection));
exit(1);
}
result = mysql_use_result(connection);//fetching result
while((row = mysql_fetch_row(result)) != NULL)//Iterating result row
{
if(strcmp(row[0], "Handler_write") == 0)
printf("Total number of Disk Writes by MySQL: %d\n", row[1]);
}
break;
case 2:
if(!CSV_flag)
printf("CSV support is not there.\n");
else
printf("CSV support is there.\n");
break;
default:printf("\nInvalid choice\n");
}
mysql_free_result(result);//Freeing the buffer
mysql_close(connection);//closing the connection
printf("\nDo you want to continue?(Y/N):");
scanf("%s",&choice);
if(choice!='Y'|| choice!='y'||choice!='n'||choice!='N')
{
printf("\nInvalid option. Program exits.\n");
exit(0);
}
printf("\n\n\n");
}while(choice =='Y' || choice =='y' );
return 0;
}
//Command to run the program: gcc -o output -I/usr/include/mysql/ -lmysqlclient InnoDB_checker.c -w