~ubuntu-branches/ubuntu/vivid/cctools/vivid

« back to all changes in this revision

Viewing changes to chirp/src/chirp_status.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2011-05-07 09:05:00 UTC
  • Revision ID: james.westby@ubuntu.com-20110507090500-lqpmdtwndor6e7os
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2003-2004 Douglas Thain and the University of Wisconsin
 
3
Copyright (C) 2005- The University of Notre Dame
 
4
This software is distributed under the GNU General Public License.
 
5
See the file COPYING for details.
 
6
*/
 
7
 
 
8
#include "catalog_query.h"
 
9
#include "nvpair.h"
 
10
#include "link.h"
 
11
#include "stringtools.h"
 
12
#include "debug.h"
 
13
 
 
14
#include <stdio.h>
 
15
#include <stdlib.h>
 
16
#include <errno.h>
 
17
#include <string.h>
 
18
 
 
19
#define MODE_TABLE 1
 
20
#define MODE_SHORT 2
 
21
#define MODE_LONG  3
 
22
#define MODE_TOTAL 4
 
23
 
 
24
static INT64_T minavail=0;
 
25
 
 
26
static struct nvpair_header headers[] = {
 
27
        { "type",    NVPAIR_MODE_STRING,  NVPAIR_ALIGN_LEFT,   8 },
 
28
        { "name",    NVPAIR_MODE_STRING,  NVPAIR_ALIGN_LEFT,  25 },
 
29
        { "port",    NVPAIR_MODE_INTEGER, NVPAIR_ALIGN_LEFT,   5 },
 
30
        { "owner",   NVPAIR_MODE_STRING,  NVPAIR_ALIGN_LEFT,  10 },
 
31
        { "version", NVPAIR_MODE_STRING,  NVPAIR_ALIGN_LEFT,   8 },
 
32
        { "total",   NVPAIR_MODE_METRIC,  NVPAIR_ALIGN_RIGHT,  8 },
 
33
        { "avail",   NVPAIR_MODE_METRIC,  NVPAIR_ALIGN_RIGHT,  8 },
 
34
        { 0, }
 
35
};
 
36
 
 
37
static void show_version( const char *cmd )
 
38
{
 
39
        printf("%s version %d.%d.%d built by %s@%s on %s at %s\n",cmd,CCTOOLS_VERSION_MAJOR,CCTOOLS_VERSION_MINOR,CCTOOLS_VERSION_MICRO,BUILD_USER,BUILD_HOST,__DATE__,__TIME__);
 
40
}
 
41
 
 
42
static void show_help( const char *cmd )
 
43
{
 
44
        printf("chirp_status [options] [ <name> <value> ]\n");
 
45
        printf("where options are:\n");
 
46
        printf(" -c <host>  Query the catalog on this host.\n");
 
47
        printf(" -d <flag>  Enable debugging for this sybsystem\n");
 
48
        printf(" -o <file>  Send debugging output to this file.\n");
 
49
        printf(" -O <bytes> Rotate file once it reaches this size.\n");
 
50
        printf(" -A <size>  Only show servers with this space available. (example: -A 100MB)\n");
 
51
        printf(" -t <time>  Timeout.\n");
 
52
        printf(" -s         Short output.\n");
 
53
        printf(" -l         Long output.\n");
 
54
        printf(" -T         Totals output.\n");
 
55
        printf(" -h         This message.\n");
 
56
        printf(" -v         Show version info.\n");
 
57
}
 
58
 
 
59
int compare_entries( struct nvpair **a, struct nvpair **b )
 
60
{
 
61
        int result;
 
62
        const char *x, *y;
 
63
 
 
64
        x = nvpair_lookup_string(*a,"type");
 
65
        if(!x) x = "unknown";
 
66
 
 
67
        y = nvpair_lookup_string(*b,"type");
 
68
        if(!y) y = "unknown";
 
69
 
 
70
        result = strcasecmp(x,y);
 
71
        if(result!=0) return result;
 
72
 
 
73
        x = nvpair_lookup_string(*a,"name");
 
74
        if(!x) x = "unknown";
 
75
 
 
76
        y = nvpair_lookup_string(*b,"name");
 
77
        if(!y) y = "unknown";
 
78
 
 
79
        return strcasecmp(x,y);
 
80
}
 
81
 
 
82
static struct nvpair *table[10000];
 
83
 
 
84
int main( int argc, char *argv[] )
 
85
{
 
86
        struct catalog_query *q;
 
87
        struct nvpair *n;
 
88
        time_t timeout=60, stoptime;
 
89
        const char * catalog_host = 0;
 
90
        char c;
 
91
        int i;
 
92
        int count=0;
 
93
        int mode = MODE_TABLE;
 
94
        INT64_T total=0, avail=0;
 
95
        const char *filter_name = 0;
 
96
        const char *filter_value = 0;
 
97
 
 
98
        debug_config(argv[0]);
 
99
 
 
100
        while((c=getopt(argc,argv,"A:c:d:t:o:O:sTlvh"))!=(char)-1) {
 
101
                switch(c) {
 
102
                        case 'c':
 
103
                                catalog_host = optarg;
 
104
                                break;
 
105
                        case 'd':
 
106
                                debug_flags_set(optarg);
 
107
                                break;
 
108
                        case 't':
 
109
                                timeout = string_time_parse(optarg);
 
110
                                break;
 
111
                        case 'A':
 
112
                                minavail = string_metric_parse(optarg);
 
113
                                break;
 
114
                        case 'o':
 
115
                                debug_config_file(optarg);
 
116
                                break;
 
117
                        case 'O':
 
118
                                debug_config_file_size(string_metric_parse(optarg));
 
119
                                break;
 
120
                        case 'v':
 
121
                                show_version(argv[0]);
 
122
                                return 1;
 
123
                        case 's':
 
124
                                mode = MODE_SHORT;
 
125
                                break;
 
126
                        case 'l':
 
127
                                mode = MODE_LONG;
 
128
                                break;
 
129
                        case 'T':
 
130
                                mode = MODE_TOTAL;
 
131
                                break;
 
132
                        case 'h':
 
133
                        default:
 
134
                                show_help(argv[0]);
 
135
                                return 1;
 
136
                }
 
137
        }
 
138
 
 
139
        if(argc-optind==0) {
 
140
                // fine, keep going
 
141
        } else if((argc-optind)==1) {
 
142
                filter_name = "name";
 
143
                filter_value = argv[optind];
 
144
        } else if((argc-optind)==2) {
 
145
                filter_name = argv[optind];
 
146
                filter_value = argv[optind+1];
 
147
        } else {
 
148
                show_help(argv[0]);
 
149
                return 1;
 
150
        }
 
151
 
 
152
        stoptime = time(0)+timeout;
 
153
 
 
154
        q = catalog_query_create(catalog_host,0,stoptime);
 
155
        if(!q) {
 
156
                fprintf(stderr,"couldn't query catalog: %s\n",strerror(errno));
 
157
                return 1;
 
158
        }
 
159
 
 
160
        if(mode==MODE_TABLE) {
 
161
                nvpair_print_table_header(stdout,headers);
 
162
        }
 
163
 
 
164
        while((n = catalog_query_read(q,stoptime))) {
 
165
                table[count++] = n;
 
166
        }
 
167
 
 
168
        qsort(table,count,sizeof(*table),(void*)compare_entries);
 
169
 
 
170
        for(i=0;i<count;i++) {
 
171
                if(minavail!=0) {
 
172
                        if(minavail>nvpair_lookup_integer(table[i],"avail")) {
 
173
                                continue;
 
174
                        }
 
175
                }
 
176
 
 
177
                if(filter_name) {
 
178
                        const char *v = nvpair_lookup_string(table[i],filter_name);
 
179
                        if(!v || strcmp(filter_value,v)) continue;
 
180
                }
 
181
 
 
182
                if(mode==MODE_SHORT) {
 
183
                        const char *t = nvpair_lookup_string(table[i],"type");
 
184
                        if(t && !strcmp(t,"chirp")) {
 
185
                                printf("%s:%d\n",nvpair_lookup_string(table[i],"name"),(int)nvpair_lookup_integer(table[i],"port"));
 
186
                        }
 
187
                } else if(mode==MODE_LONG) {
 
188
                        nvpair_print_text(table[i],stdout);
 
189
                } else if(mode==MODE_TABLE) {
 
190
                        nvpair_print_table(table[i],stdout,headers);
 
191
                } else if(mode==MODE_TOTAL) {
 
192
                        avail += nvpair_lookup_integer(table[i],"avail");
 
193
                        total += nvpair_lookup_integer(table[i],"total");
 
194
                }
 
195
        }
 
196
 
 
197
        if(mode==MODE_TOTAL) {
 
198
                printf("NODES: %4d\n",count);
 
199
                printf("TOTAL: %6sB\n",string_metric(total,-1,0));
 
200
                printf("AVAIL: %6sB\n",string_metric(avail,-1,0));
 
201
                printf("INUSE: %6sB\n",string_metric(total-avail,-1,0));
 
202
        }
 
203
 
 
204
        if(mode==MODE_TABLE) {
 
205
                nvpair_print_table_footer(stdout,headers);
 
206
        }
 
207
 
 
208
        return 0;
 
209
}