/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* bos720 src/bos/usr/ccs/lib/libperfstat/simplesspstat.c 1.3             */
/*                                                                        */
/* Licensed Materials - Property of IBM                                   */
/*                                                                        */
/* Restricted Materials of IBM                                            */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2012,2013              */
/* All Rights Reserved                                                    */
/*                                                                        */
/* US Government Users Restricted Rights - Use, duplication or            */
/* disclosure restricted by GSA ADP Schedule Contract with IBM Corp.      */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */
static char *sccsid = "@(#)69 1.3     src/bos/usr/ccs/lib/libperfstat/simplesspstat.c, libperfstat, bos720 5/8/13 05:42:28 ";

/* The sample program used to Display            *
 * the shared storage pool information           */
 
#include <stdio.h>
#include <libperfstat.h>
#include <errno.h>

/* define default interval and count values */
#define INTERVAL_DEFAULT 1
#define COUNT_DEFAULT    1

/* Check value returned by malloc for NULL */

#define CHECK_FOR_MALLOC_NULL(X) {  if ((X) == NULL) {\
                                       perror ("malloc");\
                                       exit(2);\
                                     }\
                  }

int count = COUNT_DEFAULT, interval = INTERVAL_DEFAULT;

/* store the data structures */
perfstat_ssp_t *sspstats = NULL;
int rc, returned_count, flag = 0;

/*
 * NAME: showusage
 *       to display the usage
 *
 */

void showusage()
{
    	printf("Usage:simplesspstat -s | -d | -l [-i interval] [-c count]\n");
    	exit(1);
}

/*
 * NAME: do_initialization
 *       This function initializes the data structues.
 *       It also collects initial set of values.
 *
 * RETURNS:
 * On successful completion:
 *   - returns 0.
 * In case of error
 *   - exits with code 1.
 */

int do_initialization()
{

	returned_count = perfstat_ssp(NULL, NULL, sizeof(perfstat_ssp_t),0,flag);

    if(returned_count <= 0){
	    perror("perfstat_ssp_t:");
        exit(-1);
    }
        
	/* Allocate memory for the structure*/
        sspstats=( perfstat_ssp_t  *) malloc(sizeof(perfstat_ssp_t) * returned_count);
	
	CHECK_FOR_MALLOC_NULL(sspstats);
	
    return(0);
}

static void do_cleanup()
{
	free(sspstats);
}

/*
 *Name: display_metrics
 *      collect the metrics and display them
 *
 */
void display_metrics()
{

    int i,rc;

    while (count)
    {
        /* Obtain cluster name, ssp name and vtd list*/
        rc=perfstat_ssp(NULL, sspstats, sizeof(perfstat_ssp_t),returned_count,flag);
        if(rc<0)
        {
            perror("perfstat_ssp_t:");
            exit(-1);
        }

        sleep (interval);
        fprintf(stdout, "\nCluster Name  :  %s\n", sspstats->cluster_name);
        fprintf(stdout, "Storage Pool Name  :  %s\n", sspstats->spool_name);

        if(flag == 1){
            fprintf(stdout, "Total Space  :  %lld\n", sspstats->u.global.total_space);
            fprintf(stdout, "Total Used Space  :  %lld\n", sspstats->u.global.total_used_space);
        }

        if(flag == 2){
            if(rc!=0)	
                fprintf(stdout, "Disk name  :\n");
            else
                fprintf(stdout, "There are no disks in the storage pool\n");

            for(i=0; i<returned_count; i++){
                fprintf(stdout, "%s  ",sspstats[i].u.disk.diskname);
            }   
        }   

        if(flag == 3){
            if(rc!=0)
                fprintf(stdout, "VTD name  :\n");
            else{
                fprintf(stdout, "There are no VTD's in the storage pool\n");
                exit(-1);
            } 

            for(i=0; i<returned_count; i++){
                fprintf(stdout, "%s  ",sspstats[i].u.vtd.vtd_name);
                fprintf(stdout, "%s  ",sspstats[i].u.vtd.lu_name);
            }

            fprintf(stdout,"\n\nLU type:\n");
            for(i=0; i<returned_count; i++){
                fprintf(stdout, "%s  ",sspstats[i].u.vtd.lu_type);
            }	

            fprintf(stdout,"\n\nLU size:\n");
            for(i=0; i<returned_count; i++){
                fprintf(stdout, "%lld  ",sspstats[i].u.vtd.lu_size);
            }

            fprintf(stdout,"\n\nLU free:\n"); 
            for(i=0; i<returned_count; i++){
                fprintf(stdout, "%lld  ",sspstats[i].u.vtd.lu_free);
            }
            fprintf(stdout,"\n\nLU used:\n");
            for(i=0; i<returned_count; i++){
                fprintf(stdout, "%lld  ",sspstats[i].u.vtd.lu_usage);
            }


            fprintf(stdout,"\n\n");
        } 
        count--;
    }
}

int main(int argc, char* argv[])
{
	
    	int c,sflag=0,dflag=0,lflag=0;
    	/* Enable the cluster statistics using perfstat_config */
    	rc = perfstat_config(PERFSTAT_ENABLE|PERFSTAT_CLUSTER_STATS, NULL);
    	if (rc == -1)
    	{
            perror("cluster statistics collection is not available");
            exit(-1);
    	}

	    while((c = getopt(argc, argv, "i:c:sdl"))!= EOF){
           switch(c){
                case 'i':           
                    interval = atoi(optarg);
                    if( interval <= 0 )
                        interval = INTERVAL_DEFAULT;
                    break;
                case 'c':               
                    count = atoi(optarg);
                    if( count <= 0 )
                        count = COUNT_DEFAULT;
                    break;
		        case 's':
		            flag = 1;
		            sflag = 1;	
		            break;
		        case 'd':		
		            flag = 2;
         	   	    dflag = 1;	
		            break;
		        case 'l':	
		            flag = 3;
		            lflag = 1;
		            break;	
	            default:
                    showusage(argv[0]);
	    }
	}

	if(flag == 0){
	    showusage(argv[0]);
	}

	if(sflag && (dflag || lflag)){
        showusage(argv[0]);
    } 

	if(dflag && (sflag || lflag)){
        showusage(argv[0]);
    }

	if(lflag && (sflag || dflag)){
        showusage(argv[0]);
    }

	do_initialization();
    display_metrics();
	
    /* Now disable cluster statistics by calling perfstat_config */
    perfstat_config(PERFSTAT_DISABLE|PERFSTAT_CLUSTER_STATS, NULL);

	do_cleanup();
    /*realloc(sspstats);*/
    return 0;
}