/* $Header: /afs/sipb.mit.edu/project/sipb-athena/repository/src/moira/gen/stats.dc,v 1.3 1996/06/02 07:42:32 ghudson Exp $
 *
 * This generates a list of how many lockers are on each NFS server
 * for statistics gathering purposes.
 *
 *  (c) Copyright 1991 by the Massachusetts Institute of Technology.
 *  For copying and distribution information, please see the file
 *  <mit-copyright.h>.
 */

#include <mit-copyright.h>
#include <stdio.h>
#include <moira.h>
#include <moira_site.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <strings.h>
EXEC SQL INCLUDE sqlca;

extern int errno;
char *whoami = "stats.gen";


main(argc, argv)
int argc;
char **argv;
{
    FILE *out = stdout;
    char *outf = NULL, outft[64];
    struct stat sb;
    int flag1, flag2, flag3;
    struct hash *machines;
    EXEC SQL BEGIN DECLARE SECTION;
    char name[257];
    int n, id;
    EXEC SQL END DECLARE SECTION;

#ifsql INGRES
    EXEC SQL CONNECT moira;
    EXEC SQL SET LOCKMODE SESSION WHERE LEVEL=TABLE, READLOCK=SHARED;
#endsql
#ifsql INFORMIX
    EXEC SQL DATABASE moira;
#endsql

    if (argc == 2) {
	if (stat(argv[1], &sb) == 0) {
	    if (ModDiff (&flag1, "machine", sb.st_mtime) == 0 &&
		ModDiff (&flag2, "filesys", sb.st_mtime) == 0 &&
		ModDiff (&flag3, "nfsphys", sb.st_mtime) == 0 &&
		flag1 < 0 && flag2 < 0 && flag3 < 0) {
		fprintf(stderr, "File %s does not need to be rebuilt.\n",
			argv[1]);
		exit(MR_NO_CHANGE);
	    }
	}
	outf = argv[1];
	sprintf(outft, "%s~", outf);
	if ((out = fopen(outft, "w")) == NULL) {
	    fprintf(stderr, "unable to open %s for output\n", outf);
	    exit(MR_OCONFIG);
	}
    } else if (argc != 1) {
	fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
	exit(MR_ARGS);
    } else {
	outf = NULL;
    }

    /* The following is declarative, not executed,
     * and so is dependent on where it is in the file,
     * not in the order of execution of statements.
     */
    EXEC SQL WHENEVER SQLERROR GOTO sqlerr;

    machines = create_hash(1000);

    EXEC SQL DECLARE m CURSOR FOR 
      SELECT mach_id, name 
	FROM machine;
    EXEC SQL OPEN m;
    while (1) {
      EXEC SQL FETCH m INTO :id, :name;
      if (sqlca.sqlcode != 0) break;
      hash_store(machines, id, strsave(name));
    }
    EXEC SQL CLOSE m;

    EXEC SQL DECLARE x CURSOR FOR
      SELECT DISTINCT f.mach_id, count(f.filsys_id)
      FROM filesys f
	WHERE f.type='NFS'
      GROUP BY f.mach_id;
    EXEC SQL OPEN x;
    while (1) {
	EXEC SQL FETCH x INTO :id, :n;
	if (sqlca.sqlcode != 0) break;
	fprintf(out, "%-32s %d\n", hash_lookup(machines, id), n);
    }
    EXEC SQL CLOSE x;

#ifsql INGRES
    EXEC SQL DISCONNECT;
#endsql
#ifsql INFORMIX
    EXEC SQL CLOSE DATABASE;
#endsql

    if (fclose(out)) {
	perror("close failed");
	exit(MR_CCONFIG);
    }
    if (outf)
      fix_file(outf);
    exit(MR_SUCCESS);

 sqlerr:
    com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
#ifsql INGRES
    if (sqlca.sqlcode == 17700 || sqlca.sqlcode == -37000)
      exit(MR_DEADLOCK);
#endsql
    critical_alert("DCM", "Central America build encountered INGRES ERROR %d",
		   sqlca.sqlcode);
    exit(MR_INGRES_ERR);
}
