/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* bos72D src/bos/usr/ccs/lib/libperfstat/simpleclusterdiskstat.c 1.1     */
/*                                                                        */
/* Licensed Materials - Property of IBM                                   */
/*                                                                        */
/* Restricted Materials of IBM                                            */
/*                                                                        */
/* COPYRIGHT International Business Machines Corp. 2015                   */
/* 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 = "@(#)93  1.1 src/bos/usr/ccs/lib/libperfstat/simpleclusterdiskstat.c, libperfstat, bos72D, d2016_05A2 12/18/15 00:56:51";

#include <stdio.h>
#include <libperfstat.h>

typedef enum {
	DISPLAY_NODE_DATA = 1,
	DISPLAY_DISK_DATA = 2,
} display_t;

int main(int argc, char* argv[])
{
	perfstat_node_data_t *node_details;
	perfstat_disk_data_t *disk_details;
	perfstat_id_node_t nodeid;
	char nodename[MAXHOSTNAMELEN];
    display_t display = DISPLAY_DISK_DATA;
	int num_nodes;
	int i, rc, num_of_disks = 0;

	/* Process the arguments */
	while ((i = getopt(argc, argv, "n:d")) != EOF)
	{
		switch(i)
		{
			case 'n':               /* Request to display node data */
				display |= DISPLAY_NODE_DATA;
				strncpy(nodename,optarg,MAXHOSTNAMELEN);
				break;
			case 'd':               /* Request to diplay disk data */
				display |= DISPLAY_DISK_DATA;
				break;

			case 'h':               /* Print help message */
			default:
				/* Print the usage and terminate */
				fprintf (stderr, "usage: %s [-n <nodename>] [-d]\n", argv[0]);
				exit(-1);
		}
	}

	/* perfstat_config needs to be called to enable cluster statistics collection */
	rc = perfstat_config(PERFSTAT_ENABLE|PERFSTAT_CLUSTER_STATS, NULL);
	if (rc == -1)
	{
		perror("cluster statistics collection is not available");
		exit(-1);
	}
	/*If Node details are specified pass that data as input to get the disk details
	  for that node . Else pass FIRST_NODENAME */
	if (display & DISPLAY_NODE_DATA)
	{
		strncpy(nodeid.u.nodename,nodename,MAXHOSTNAMELEN);
	}
	else
		strncpy(nodeid.u.nodename, FIRST_NODENAME, MAXHOSTNAMELEN);

	nodeid.spec = NODENAME;
	/*Get the number of disks for that node */
	num_of_disks = perfstat_cluster_disk(&nodeid,NULL, sizeof(perfstat_disk_data_t), 0);    
	if (num_of_disks == -1)
	{
		perror("perfstat_cluster_disk failed");
		exit(-1);
	}

	disk_details = (perfstat_disk_data_t *)calloc(num_of_disks,sizeof(perfstat_disk_data_t));
	/* collect all the disk data for the node */
	num_of_disks = perfstat_cluster_disk(&nodeid,disk_details,sizeof(perfstat_disk_data_t),num_of_disks);
	fprintf(stdout, "Disk Details\n");
	fprintf(stdout, "------------------\n");
	for(i = 0; i < num_of_disks; i++)
	{
		fprintf(stdout,"Disk Name:%s\t UDID:%s\n",disk_details[i].name,disk_details[i].uuid);

	}

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