]> git.neil.brown.name Git - TABS.git/blob - lablist/lab.c
Terminal Alocation and Booking System - June 2006
[TABS.git] / lablist / lab.c
1
2 #include        "lablist.h"
3 /*#include <signal.h>*/
4 #include <unistd.h>
5
6 /*
7  * lab: print out status of some labs.
8  * possibly with heading, possibly only down or outofservice machines
9  */
10
11 char *periodname[] = { "<null>", "FREE", "FULL", "CLOSED" };
12 char *nstatus[] = { "<null>", "Up", "Down"};
13 char *bstatus[] = { "<null>", "OutOfService", "Broken", "NotBookable",
14                 "UnAllocated", "Allocated", "Tentative" };
15
16 enum { N_UP, N_DOWN, N_OUT, N_ALL } class; /* which workstations to show */
17 bool_t show_header;
18 bool_t phost;
19
20 void print_lab(labinfo li)
21 {
22         int i;
23         if (show_header)
24         {
25                 char *name, *host;
26                 int xdrname = 1, xdrhost = 1;
27                 name = get_mappingchar(li->labid, M_ATTRIBUTE);
28                 if (name == NULL) {
29                         name= "<unknown>";
30                         xdrname = 0;
31                 }
32                 host = get_mappingchar(alloc_host(li->alloc), M_HOST);
33                 if (host == NULL) {
34                         host = "<unknown>";
35                         xdrhost = 0;
36                 }
37                 if (li->alloc->data.data_len <3)
38                         printf("\nLab %s is %s: no allocations\n\n",
39                                name, periodname[alloc_status(li->alloc)]);
40                 else
41                 {
42                         time_t period = cluster_modtime(li->alloc);
43                         int doy, pod;
44                         char doys[10], pods[10];
45                         get_doypod(&period, &doy, &pod);
46                         printf("\nLab %s is %s, last allocation by %s for %s %s (at %s)\n\n",
47                                name, periodname[alloc_status(li->alloc)],
48                                host,
49                                doy2str(doys, doy), pod2str(pods, pod),
50                                myctime(alloc_when(li->alloc))
51                                 );
52                 }
53                 if (xdrname)
54                         free(name);
55                 if (xdrhost)
56                         free(host);
57         }
58
59         for (i=0 ; i < li->wscnt ; i++)
60         {
61                 workstation w = &li->wslist[i];
62                 if (
63                         ((class==N_UP) && ws_status(w->status) == NODEUP)
64                         || ((class==N_DOWN) && ws_status(w->status) == NODEDOWN)
65                         || ((class==N_OUT) && res_status(w->alloc) == NODEOUTOFSERVICE)
66                         || (class== N_ALL)
67                         )
68                 {
69                         char *wsname;
70                         char *useron = NULL;
71                         char *userres = NULL;
72                         char c1 = ' ' , c2= ' ' ;
73
74                         wsname = get_mappingchar(w->wsid, M_WORKSTATION);
75                         if (wsname == NULL) wsname = strdup("<unknown>");
76                         if (ws_user(w->status) >= 0)
77                         {
78                                 useron = find_username(ws_user(w->status));
79                                 if (useron == NULL) useron = strdup("<who?>");
80                         }
81                         if (res_user(w->alloc) >= 0)
82                         {
83                                 userres = find_username(res_user(w->alloc));
84                                 if (userres == NULL) userres = strdup("<who?>");
85                                 c1 = '{' ; c2 = '}';
86                                 if (in_class(ws_user(w->status), res_user(w->alloc))==1)
87                                         c1='[', c2=']';
88                         }
89
90                         printf("%-12s:%-8s%12s: %-10s %c %-9s%c",
91                                wsname, nstatus[ws_status(w->status)],
92                                bstatus[res_status(w->alloc)],
93                                useron?useron:"",
94                                c1, userres?userres:"", c2);
95
96                         if (phost)
97                         {
98                                 int h = ws_whereon(w->status);
99                                 if (h >0)
100                                 {
101                                         char *hn = get_mappingchar(h, M_HOST);
102                                         if (hn)
103                                                 printf(" on %s", hn);
104                                         else
105                                                 printf("on host-%d", h);
106                                 }
107                         }
108                         else
109                         {
110                                 /* print login time */
111                                 if (ws_whenon(w->status))
112                                         printf(" since %s",
113                                                myctime(ws_whenon(w->status)));
114                         }
115                         printf("\n");
116                         if (class == N_OUT)
117                         {
118                                 workstation_state ws;
119                                 char k[20];
120                                 memset(&ws, 0, sizeof(ws));
121                                 if (db_read(key_int(k, C_WORKSTATIONS, w->wsid), &ws,
122                                             (xdrproc_t)xdr_workstation_state, CONFIG) == R_RESOK)
123                                 {
124                                         if (ws.why)
125                                                 printf(" %s (%s)\n", ws.why,
126                                                        myctime(ws.time));
127                                         xdr_free((xdrproc_t)xdr_workstation_state, (char*) &ws);
128                                 }
129                         }
130                         if (useron) free(useron);
131                         if (userres) free(userres);
132                         free(wsname);
133                 }
134         }
135 }
136
137 char *Usage[] = {
138     "Usage: lab [-udosASht] labname ...",
139     "  -u : only list UP workstations",
140     "  -d : only list DOWN workstations",
141     "  -o : only list out of service workstations, and give reason",
142     "  -s : don't print header on lab listing",
143     "  -t : show time of login (default)",
144     "  -h : show host that ws is logged on to instead of time",
145     "  -A : list all labs",
146     "  -S : list all bookable labs",
147     "  -R : repeat listing ever 30 seconds",
148     "  labname: the name of any lab of group of labs",
149     NULL
150 };
151
152 int main(int argc, char *argv[])
153 {
154         extern int optind;
155         extern char *optarg;
156         char *msg;
157         int opt;
158         void *lablist = lablist_make();
159         labinfo li;
160         int repeat = FALSE;
161
162         /* quit on receipt of sigusr1 - to help with memory debugging in loops */
163 /*
164   struct sigaction sa;
165   sigset_t ss;
166   sa.sa_handler = exit;
167   sa.sa_flags = 0;
168   sigemptyset(&ss);
169   sigaddset(&ss, SIGUSR1);
170   sa.sa_mask = ss;
171   sigaction(SIGUSR1, &sa, NULL);
172 */
173         class = N_ALL;
174         show_header = TRUE;
175         phost = FALSE;
176         while ((opt = getopt(argc, argv, "udosASRht"))!= EOF)
177                 switch(opt)
178                 {
179                 case 'h': phost = TRUE; break;
180                 case 't': phost = FALSE; break;
181                 case 'u': class = N_UP ; break ;
182                 case 'd': class = N_DOWN ; break ;
183                 case 'o': class = N_OUT ; break ;
184                 case 's': show_header = FALSE ; break ;
185                 case 'R': repeat = TRUE; break;
186                 case 'A': 
187                         msg = add_lab(lablist, NULL);
188                         if (msg)
189                         {
190                                 fprintf(stderr, "lab: could not find all labs: %s\n", msg);
191                                 exit(1);
192                         }
193                         break;
194                 case 'S':
195                         msg = add_lab(lablist, "bookable");
196                         if (msg)
197                         {
198                                 fprintf(stderr, "lab: could not find bookable labs: %s\n", msg);
199                                 exit(1);
200                         }
201                         break;
202                 default:
203                         usage();
204                         exit(1);
205                 }
206         for (; optind < argc ; optind++)
207         {
208                 msg = add_lab(lablist, argv[optind]);
209                 if (msg)
210                 {
211                         fprintf(stderr, "lab: could not add lab %s: %s\n", argv[optind], msg);
212                         exit(1);
213                 }
214         }
215         if (lablist_empty(lablist))
216         {
217                 fprintf(stderr, "lab: no labs given\n");
218                 exit(0);
219         }
220
221
222         do {
223                 load_labs(lablist);
224
225                 for (li = lablist_next(lablist) ; li != lablist ; li = lablist_next(li))
226                         if (li->alloc)
227                                 print_lab(li);
228                         else
229                                 printf("Cannot get table for %s\n", li->labname);
230                 if (repeat) {
231                         printf("---------------------\n");
232                         fflush(stdout);
233                         sleep(30);
234                 }
235         } while (repeat);
236         exit(0);
237 }