/[pdpsoft]/trunk/nl.nikhef.ndpf.tools/nodelist/nodelist.pl
ViewVC logotype

Contents of /trunk/nl.nikhef.ndpf.tools/nodelist/nodelist.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2782 - (show annotations) (download) (as text)
Fri Apr 24 11:43:32 2015 UTC (6 years, 7 months ago) by dennisvd
File MIME type: text/x-prolog
File size: 4479 byte(s)
add simple script to list nodes based on name or attributes

1 #!/usr/bin/perl -w
2
3 # Copyright 2014 Stichting FOM
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # This program implements a set of utility functions that may be
18 # used by maintainer scripts for VO support purposes
19
20 =head1 NAME
21
22 nodelist - utility to build lists of Torque worker nodes
23
24 =head1 SYNOPSIS
25
26 nodelist -nodematch regex
27
28 nodelist -propertymatch 'property:regex'
29
30 nodelist -propertymatch 'property!regex'
31
32 =head1 DESCRIPTION
33
34 This utility prints a list of Torque worker nodes, matching certain
35 properties. It uses the output of the `pbsnodes' command to gather
36 information.
37
38 The simplest form of matching is based on the names of the worker
39 nodes with the B<-nodematch> option. All matching is
40 done through Perl regular expressions (see L<perlre(1)>). Basic string
41 matching works but be aware of the special meaning of characters such as
42 `.', `(', `)', `[', `]', `?', `*', `^', `$', etc.
43
44 Matching based on properties works by passing property name followed
45 by a colon and a regular expression.
46
47 To exclude machines with a certain property match, use a `!' instead
48 of a `:'.
49
50 Node matching and property matching may be combined.
51
52 =head1 EXAMPLES
53
54 Print all nodes that are not currently down:
55
56 nodelist -p 'state!down'
57
58 Print nodes without jobs:
59
60 nodelist -p 'jobs!'
61
62 Print nodes running a certain kernel version:
63
64 nodelist -p 'status:uname=.*2\.6\.32-431\.17\.1'
65
66 =head1 COPYRIGHT AND LICENSE
67
68 Copyright 2012 Stichting FOM
69
70 Licensed under the Apache License, Version 2.0 (the "License");
71 you may not use this file except in compliance with the License.
72 You may obtain a copy of the License at
73
74 L<http://www.apache.org/licenses/LICENSE-2.0>
75
76 Unless required by applicable law or agreed to in writing, software
77 distributed under the License is distributed on an "AS IS" BASIS,
78 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
79 See the License for the specific language governing permissions and
80 limitations under the License.
81
82 =cut
83 use strict;
84
85 # parse pbsnodes output to find nodes matching certain properties
86
87 use Pod::Usage;
88 use Getopt::Long;
89
90 my $nodematch = "";
91 my $propmatch = undef; # format: "property:regex" or "property!regex" to negate
92 my $pnegate = 0; # whether ! was used.
93 my $debug = 0;
94 my $help = 0;
95
96 my $optres = GetOptions("nodematch=s" => \$nodematch,
97 "propertymatch=s" => \$propmatch,
98 "help" => \$help,
99 "debug" => \$debug);
100 if (! $optres) {
101 pod2usage("Error parsing command line options.\n");
102 };
103
104 if ($help) {
105 pod2usage(-verbose => 2, -exitval => 0);
106 }
107
108 my $prop = undef; # property to search for
109 my $pval = ""; # value of property to match (re)
110
111 if (defined $propmatch) {
112 if ($propmatch =~ /(\w+)([:!])(.*)/) {
113 $prop = $1;
114 $pval = $3;
115 $pnegate = ( $2 eq "!" );
116 } else {
117 pod2usage("propertymatch should be either 'property:value' or 'property!value'\n");
118 }
119 }
120
121 open(PBSNODES, "pbsnodes |") || die "can't run pbsnodes: $!";
122
123 my $node = undef;
124 my %nodeprop;
125
126 while(<PBSNODES>) {
127 if (/^(\S+)/) {
128 $node = $1;
129 $nodeprop{"$node"} = {};
130 next;
131 }
132 if (/ (\w+) = (.*)/) {
133 $nodeprop{"$node"}{"$1"} = "$2";
134 }
135 }
136
137 close(PBSNODES);
138
139 # search by node (re match)
140
141 print "nodematch: '$nodematch'\n" if $debug;
142
143 if ($nodematch eq "") {
144 $nodematch = "."; # match anything
145 }
146
147 for my $n (keys %nodeprop) {
148 if ($n =~ /$nodematch/) {
149 print "match node $n\n" if $debug;
150 # search by property (re match)
151 if ($debug && ref($nodeprop{$n}) eq "HASH") {
152 for my $m (keys(%{$nodeprop{$n}}) ) {
153 print "prop: $m\n";
154 print "value: " . $nodeprop{"$n"}{"$m"} . "\n";
155 }
156 }
157 # Here comes the ugly boolean logic.
158 if (!defined($prop) # It's ok if no property match was requested at all
159 || $pnegate != # otherwise the negation of the match should logically not be
160 ( defined($nodeprop{"$n"}{"$prop"}) && ($nodeprop{"$n"}{"$prop"} =~ /$pval/)) # the outcome of the match.
161 ) {
162 print "$n\n";
163 }
164 } else {
165 print "no match for node $n\n" if $debug;
166 }
167 }

grid.support@nikhef.nl
ViewVC Help
Powered by ViewVC 1.1.28