]> andersk Git - moira.git/blob - gen/stats.dc
New database and column names for Moira2.
[moira.git] / gen / stats.dc
1 /* $Header$
2  *
3  * This generates a list of how many lockers are on each NFS server
4  * for statistics gathering purposes.
5  *
6  *  (c) Copyright 1991 by the Massachusetts Institute of Technology.
7  *  For copying and distribution information, please see the file
8  *  <mit-copyright.h>.
9  */
10
11 #include <mit-copyright.h>
12 #include <stdio.h>
13 #include <moira.h>
14 #include <moira_site.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <sys/time.h>
18 #include <strings.h>
19 EXEC SQL INCLUDE sqlca;
20
21 extern int errno;
22 char *whoami = "stats.gen";
23
24
25 main(argc, argv)
26 int argc;
27 char **argv;
28 {
29     FILE *out = stdout;
30     char *outf = NULL, outft[64];
31     struct stat sb;
32     int flag1, flag2, flag3;
33     struct hash *machines;
34     EXEC SQL BEGIN DECLARE SECTION;
35     char name[257];
36     int n, id;
37     EXEC SQL END DECLARE SECTION;
38
39 #ifsql INGRES
40     EXEC SQL CONNECT moira;
41 #endsql
42 #ifsql INFORMIX
43     EXEC SQL DATABASE moira;
44 #endsql
45
46     if (argc == 2) {
47         if (stat(argv[1], &sb) == 0) {
48             if (ModDiff (&flag1, "machine", sb.st_mtime) == 0 &&
49                 ModDiff (&flag2, "filesys", sb.st_mtime) == 0 &&
50                 ModDiff (&flag3, "nfsphys", sb.st_mtime) == 0 &&
51                 flag1 < 0 && flag2 < 0 && flag3 < 0) {
52                 fprintf(stderr, "File %s does not need to be rebuilt.\n",
53                         argv[1]);
54                 exit(MR_NO_CHANGE);
55             }
56         }
57         outf = argv[1];
58         sprintf(outft, "%s~", outf);
59         if ((out = fopen(outft, "w")) == NULL) {
60             fprintf(stderr, "unable to open %s for output\n", outf);
61             exit(MR_OCONFIG);
62         }
63     } else if (argc != 1) {
64         fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
65         exit(MR_ARGS);
66     } else {
67         outf = NULL;
68     }
69
70     /* The following is declarative, not executed,
71      * and so is dependent on where it is in the file,
72      * not in the order of execution of statements.
73      */
74     EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
75
76     machines = create_hash(1000);
77
78     EXEC SQL DECLARE m CURSOR FOR 
79       SELECT mach_id, name 
80         FROM machine;
81     EXEC SQL OPEN m;
82     while (1) {
83       EXEC SQL FETCH m INTO :id, :name;
84       if (sqlca.sqlcode != 0) break;
85       hash_store(machines, id, strsave(name));
86     }
87     EXEC SQL CLOSE m;
88
89     EXEC SQL DECLARE x CURSOR FOR
90       SELECT DISTINCT f.mach_id, count(f.filsys_id)
91       FROM filesys f
92         WHERE f.type='NFS'
93       GROUP BY f.mach_id;
94     EXEC SQL OPEN x;
95     while (1) {
96         EXEC SQL FETCH x INTO :id, :n;
97         if (sqlca.sqlcode != 0) break;
98         fprintf(out, "%s %d\n", hash_lookup(machines, id), n);
99     }
100     EXEC SQL CLOSE x;
101
102 #ifsql INGRES
103     EXEC SQL DISCONNECT;
104 #endsql
105 #ifsql INFORMIX
106     EXEC SQL CLOSE DATABASE;
107 #endsql
108
109     if (fclose(out)) {
110         perror("close failed");
111         exit(MR_CCONFIG);
112     }
113     if (outf)
114       fix_file(outf);
115     exit(MR_SUCCESS);
116
117  sqlerr:
118     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
119     critical_alert("DCM", "Central America build encountered INGRES ERROR %d",
120                    sqlca.sqlcode);
121     exit(MR_INGRES_ERR);
122 }
This page took 0.047259 seconds and 5 git commands to generate.