]> andersk Git - moira.git/blame - gen/stats.dc
Diane Delgado's changes for a fixed table-locking order
[moira.git] / gen / stats.dc
CommitLineData
0966656e 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>
19EXEC SQL INCLUDE sqlca;
20
21extern int errno;
22char *whoami = "stats.gen";
23
24
25main(argc, argv)
26int argc;
27char **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
968f523c 40 EXEC SQL CONNECT moira;
41 EXEC SQL SET LOCKMODE SESSION WHERE LEVEL=TABLE, READLOCK=SHARED;
0966656e 42#endsql
43#ifsql INFORMIX
968f523c 44 EXEC SQL DATABASE moira;
0966656e 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
968f523c 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 */
0966656e 75 EXEC SQL WHENEVER SQLERROR GOTO sqlerr;
76
741abed8 77 EXEC SQL SELECT modtime INTO :name FROM filesys WHERE filsys_id = 0;
78 EXEC SQL SELECT modtime INTO :name FROM machine WHERE mach_id = 0;
79
80
0966656e 81 machines = create_hash(1000);
82
741abed8 83
84
0966656e 85 EXEC SQL DECLARE m CURSOR FOR
86 SELECT mach_id, name
87 FROM machine;
88 EXEC SQL OPEN m;
89 while (1) {
90 EXEC SQL FETCH m INTO :id, :name;
91 if (sqlca.sqlcode != 0) break;
92 hash_store(machines, id, strsave(name));
93 }
94 EXEC SQL CLOSE m;
95
96 EXEC SQL DECLARE x CURSOR FOR
97 SELECT DISTINCT f.mach_id, count(f.filsys_id)
98 FROM filesys f
968f523c 99 WHERE f.type='NFS'
0966656e 100 GROUP BY f.mach_id;
101 EXEC SQL OPEN x;
102 while (1) {
103 EXEC SQL FETCH x INTO :id, :n;
104 if (sqlca.sqlcode != 0) break;
968f523c 105 fprintf(out, "%-32s %d\n", hash_lookup(machines, id), n);
0966656e 106 }
107 EXEC SQL CLOSE x;
108
109#ifsql INGRES
110 EXEC SQL DISCONNECT;
111#endsql
112#ifsql INFORMIX
113 EXEC SQL CLOSE DATABASE;
114#endsql
115
116 if (fclose(out)) {
117 perror("close failed");
118 exit(MR_CCONFIG);
119 }
120 if (outf)
121 fix_file(outf);
122 exit(MR_SUCCESS);
123
124 sqlerr:
125 com_err(whoami, MR_INGRES_ERR, " code %d\n", sqlca.sqlcode);
dcc259bc 126#ifsql INGRES
127 if (sqlca.sqlcode == 17700 || sqlca.sqlcode == -37000)
128 exit(MR_DEADLOCK);
129#endsql
741abed8 130 critical_alert("DCM", "Stats build encountered INGRES ERROR %d",
0966656e 131 sqlca.sqlcode);
132 exit(MR_INGRES_ERR);
133}
This page took 0.078663 seconds and 5 git commands to generate.