1 #!/usr/bin/perl
2 # tinderbox_build.pl
3 # This script builds the nagiosplugins and then sends
4 # logs back to the master tinderbox server
5 #
6 # This script is based on mozilla-unix.pl which comes with tinderbox2
7 #
8 # See http://tinderbox.altinity.org for more details
10 require 5.000;
12 use strict;
13 use Sys::Hostname;
14 use Cwd;
15 use Time::Local;
17 my $Version = '$Revision$';
19 my $myhost = hostname;
20 chomp($myhost);
21 my ($host, $junk) = split(/\./, $myhost);
23 my $BuildAdministrator = $ENV{TINDERBOX_BUILD_ADMIN} || "$ENV{'USER'}\@$myhost";
25 #Default values of cmdline opts
26 my $ReportStatus = 0; # Do not send results to server
28 # Set these to what makes sense for your system
30 # Set these proper values for your tinderbox server
31 my $Tinderbox_server = 'tinderbox2@tinderbox.altinity.org';
33 # These shouldn't really need to be changed
34 my $BuildTree = 'nagiosplug';
35 my $BuildName = '';
36 my $ConfigureArgs = $ENV{CONFIGURE_ARGS};
38 my $OS = `uname -s`;
39 my $OSVer = `uname -r`;
41 chop($OS, $OSVer);
43 if ( $OS eq 'AIX' ) {
44 $OSVer = `uname -v`;
45 chop($OSVer);
46 $OSVer = $OSVer . "." . `uname -r`;
47 chop($OSVer);
48 }
50 if ( $OS eq 'IRIX64' ) {
51 $OS = 'IRIX';
52 }
54 if ( $OS eq 'SCO_SV' ) {
55 $OS = 'SCOOS';
56 $OSVer = '5.0';
57 }
59 if ( "$host" ne "" ) {
60 $BuildName = $host . ' ';
61 }
62 $BuildName .= $OS . ' ' . $OSVer;
63 $_ = $BuildName;
64 s/ /_/g;
66 my $logfile = "$_.log";
68 sub BuildIt {
69 my ($fe, @felist, $EarlyExit, $LastTime);
71 my $StartDir = getcwd();
72 $LastTime = 0;
74 print "Starting dir is : $StartDir\n";
76 my $EarlyExit = 0;
78 chdir("$StartDir");
80 my $StartTime = time;
81 if (-e (my $file = "nagios-plugins.spec")) {
82 open F, $file;
83 while (<F>) {
84 if (/^Version: HEAD-(\d\d\d\d)(\d\d)(\d\d)(\d\d)(\d\d)$/) {
85 $StartTime = timelocal(0, $5, $4, $3, ($2 - 1), ($1 - 1900));
86 last;
87 }
88 }
89 }
91 print "Start time is $StartTime",$/;
93 my $CurrentDir = getcwd();
94 if ( $CurrentDir ne $StartDir ) {
95 print "startdir: $StartDir, curdir $CurrentDir\n";
96 die "curdir != startdir";
97 }
99 unlink( "$logfile" );
101 print "opening $logfile\n";
102 open( LOG, ">$logfile" ) || print "can't open $?\n";
103 print LOG "current dir is -- $host:$CurrentDir\n";
104 print LOG "Build Administrator is $BuildAdministrator\n";
105 &PrintEnv;
107 my $BuildStatus;
108 if (&configure) {
109 if (&make) {
110 if (&maketest) {
111 $BuildStatus = "success";
112 } else {
113 $BuildStatus = "test_failed";
114 }
115 } else {
116 $BuildStatus = "build_failed";
117 }
118 } else {
119 $BuildStatus = "busted";
120 }
122 print LOG "\nBuild Status = $BuildStatus\n";
124 close(LOG);
125 chdir("$StartDir");
127 # TV: Leaving this in, because process mail program probably has some
128 # limitation retained
130 # this fun line added on 2/5/98. do not remove. Translated to english,
131 # that's "take any line longer than 1000 characters, and split it into less
132 # than 1000 char lines. If any of the resulting lines is
133 # a dot on a line by itself, replace that with a blank line."
134 # This is to prevent cases where a <cr>.<cr> occurs in the log file. Sendmail
135 # interprets that as the end of the mail, and truncates the log before
136 # it gets to Tinderbox. (terry weismann, chris yeh)
137 #
138 # This was replaced by a perl 'port' of the above, writen by
139 # preed@netscape.com; good things: no need for system() call, and now it's
140 # all in perl, so we don't have to do OS checking like before.
142 open(LOG, "$logfile") || die "Couldn't open logfile: $!\n";
143 open(OUTLOG, ">${logfile}.last") || die "Couldn't open logfile: $!\n";
145 print OUTLOG $/;
146 print OUTLOG "tinderbox: tree: $BuildTree\n";
147 print OUTLOG "tinderbox: builddate: $StartTime\n";
148 print OUTLOG "tinderbox: status: $BuildStatus\n";
149 print OUTLOG "tinderbox: build: $BuildName $fe\n";
150 print OUTLOG "tinderbox: errorparser: unix\n";
151 print OUTLOG "tinderbox: buildfamily: unix\n";
152 print OUTLOG "tinderbox: END\n";
153 print OUTLOG $/;
155 while (<LOG>) {
156 my $q = 0;
158 for (;;) {
159 my $val = $q * 1000;
160 my $Output = substr($_, $val, 1000);
162 last if $Output eq undef;
164 $Output =~ s/^\.$//g;
165 $Output =~ s/\n//g;
166 print OUTLOG "$Output\n";
167 $q++;
168 } #EndFor
170 } #EndWhile
172 close(LOG);
173 close(OUTLOG);
175 if ($ReportStatus) {
176 system( "ssh $Tinderbox_server tinderbox_receive < ${logfile}.last" )
177 } else {
178 print "Not sending logs to tinderbox",$/;
179 }
181 unlink("$logfile");
182 print "Finished building for tinderbox",$/;
184 } #EndSub-BuildIt
186 sub ParseArgs {
187 my($i);
189 $i = 0;
190 while( $i < @ARGV ) {
191 if ($ARGV[$i] eq '--version' || $ARGV[$i] eq '-v') {
192 die "$0: version $Version\n";
193 } elsif ($ARGV[$i] eq '-y') {
194 $ReportStatus = 1;
195 } else {
196 &PrintUsage;
197 }
199 $i++;
200 } #EndWhile
202 } #EndSub-ParseArgs
204 sub PrintUsage {
205 die "usage: $0 [-v | --version ] [-t do not send report to tinderbox server]\n";
206 }
208 sub PrintEnv {
209 my ($key);
210 foreach $key (keys %ENV) {
211 print LOG "$key = $ENV{$key}\n";
212 print "$key = $ENV{$key}\n";
213 }
215 # Print the NPTest variables
216 if (-e "/var/tmp/NPTest.cache") {
217 open F, "/var/tmp/NPTest.cache";
218 print LOG "NPTest variables:\n";
219 print LOG <F>;
220 close F;
221 }
223 } #EndSub-PrintEnv
225 sub SetupPath {
226 my($Path);
227 $Path = $ENV{PATH};
228 print "Path before: $Path\n";
230 if ( $OS eq 'SunOS' ) {
231 $ENV{'PATH'} = '/usr/ccs/bin:' . $ENV{'PATH'};
232 }
234 $Path = $ENV{PATH};
235 print "Path After: $Path\n";
236 } #EndSub-SetupPath
238 sub configure {
239 # Configure
240 print LOG "./configure $ConfigureArgs\n";
241 open (CONFIGURE, "./configure $ConfigureArgs 2>&1 |") || die "../configure: $!\n";
242 while (<CONFIGURE>) {
243 print $_;
244 print LOG $_;
245 }
246 close(CONFIGURE);
247 return ! $?;
248 }
250 sub make {
251 # Building
252 print LOG "make 2>&1\n";
253 open( MAKE, "make 2>&1 |");
254 while ( <MAKE> ) {
255 print $_;
256 print LOG $_;
257 }
258 close( MAKE);
259 return ! $?;
260 }
262 sub maketest {
263 # Tests
264 print LOG "LANG=C make test 2>&1\n";
265 open( MAKE, "LANG=C make test 2>&1 |");
266 while ( <MAKE> ) {
267 print $_;
268 print LOG $_;
269 }
270 close( MAKE);
271 return ! $?;
272 }
274 # Main function
275 &ParseArgs;
276 &SetupPath;
277 &BuildIt;
279 1;