]> andersk Git - moira.git/blob - gen/stats.dc
Solaris/POSIX changes
[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     EXEC SQL SET LOCKMODE SESSION WHERE LEVEL=TABLE, READLOCK=SHARED;
42 #endsql
43 #ifsql INFORMIX
44     EXEC SQL DATABASE moira;
45 #endsql
46
47     if (argc == 2) {
48         if (stat(argv[1], &sb) == 0) {
49             if (ModDiff (&flag1, "machine", sb.st_mtime) == 0 &&
50                 ModDiff (&flag2, "filesys", sb.st_mtime) == 0 &&
51                 ModDiff (&flag3, "nfsphys", sb.st_mtime) == 0 &&
52                 flag1 < 0 && flag2 < 0 && flag3 < 0) {
53                 fprintf(stderr, "File %s does not need to be rebuilt.\n",
54                         argv[1]);
55                 exit(MR_NO_CHANGE);
56             }
57         }
58         outf = argv[1];
59         sprintf(outft, "%s~", outf);
60         if ((out = fopen(outft, "w")) == NULL) {
61             fprintf(stderr, "unable to open %s for output\n", outf);
62             exit(MR_OCONFIG);
63         }
64     } else if (argc != 1) {
65         fprintf(stderr, "usage: %s [outfile]\n", argv[0]);
66         exit(MR_ARGS);
67     } else {
68         outf = NULL;
69     }
70
71     /* The following is declarative, not executed,
72      * and so is dependent on where it is in the file,
73      * not in the order of execution of statements.
74      */
75     EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
76
77     machines = create_hash(1000);
78
79     EXEC SQL DECLARE m CURSOR FOR 
80       SELECT mach_id, name 
81         FROM machine;
82     EXEC SQL OPEN m;
83     while (1) {
84       EXEC SQL FETCH m INTO :id, :name;
85       if (sqlca.sqlcode != 0) break;
86       hash_store(machines, id, strsave(name));
87     }
88     EXEC SQL CLOSE m;
89
90     EXEC SQL DECLARE x CURSOR FOR
91       SELECT DISTINCT f.mach_id, count(f.filsys_id)
92       FROM filesys f
93         WHERE f.type='NFS'
94       GROUP BY f.mach_id;
95     EXEC SQL OPEN x;
96     while (1) {
97         EXEC SQL FETCH x INTO :id, :n;
98         if (sqlca.sqlcode != 0) break;
99         fprintf(out, "%-32s %d\n", hash_lookup(machines, id), n);
100     }
101     EXEC SQL CLOSE x;
102
103 #ifsql INGRES
104     EXEC SQL DISCONNECT;
105 #endsql
106 #ifsql INFORMIX
107     EXEC SQL CLOSE DATABASE;
108 #endsql
109
110     if (fclose(out)) {
111         perror("close failed");
112         exit(MR_CCONFIG);
113     }
114     if (outf)
115       fix_file(outf);
116     exit(MR_SUCCESS);
117
118  sqlerr:
119     com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
120 #ifsql INGRES
121     if (sqlca.sqlcode == 17700 || sqlca.sqlcode == -37000)
122       exit(MR_DEADLOCK);
123 #endsql
124     critical_alert("DCM", "Central America build encountered INGRES ERROR %d",
125                    sqlca.sqlcode);
126     exit(MR_INGRES_ERR);
127 }
This page took 0.1095 seconds and 5 git commands to generate.