-
Notifications
You must be signed in to change notification settings - Fork 2
/
cluster_status.php
174 lines (137 loc) · 4.4 KB
/
cluster_status.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
{};
$us3bin = exec( "ls -d ~us3/lims/bin" );
require "$us3bin/listen-config.php";
require "$us3bin/cluster_config.php";
$debug = false;
$no_db_updates = false;
function debug_json( $msg, $json ) {
global $debug;
if ( !isset( $debug ) || !$debug ) {
return;
}
fwrite( STDERR, "$msg\n" );
fwrite( STDERR, json_encode( $json, JSON_PRETTY_PRINT ) );
fwrite( STDERR, "\n" );
}
function error_exit( $msg ) {
global $self;
fwrite( STDERR, "$self: $msg\nTerminating due to errors.\n" );
exit(-1);
}
function run_cmd( $cmd, $die_if_exit = true, $array_result = false ) {
global $debug;
if ( isset( $debug ) && $debug ) {
echo "$cmd\n";
}
exec( "$cmd 2>&1", $res, $res_code );
if ( $die_if_exit && $res_code ) {
error_exit( "shell command '$cmd' returned result:\n" . implode( "\n", $res ) . "\nand with exit status '$res_code'" );
}
if ( !$array_result ) {
return implode( "\n", $res ) . "\n";
}
return $res;
}
$data = array();
local_status();
if ( $no_db_updates ) {
error_exit( "no db updates set, so exiting now" );
}
$gfac_link = mysqli_connect( $dbhost, $guser, $gpasswd, $gDB );
if ( ! $gfac_link ) {
error_exit( "Could not connect to DB $gDB" );
}
foreach ( $data as $item ) {
update( $item[ 'cluster' ], $item[ 'queued' ], $item[ 'status' ], $item[ 'running' ] );
}
mysqli_close( $gfac_link );
exit(0);
## Put it in the DB
function update( $cluster, $queued, $status, $running ) {
global $gfac_link;
## added time=CURRENT_TIMESTAMP() on updates since mariadb (10.3.28)
## doesn't seem to honor the gfac.cluster_status.time on update current_timestamp()
## if we put a primary key on gfac.cluster_status.cluster we could use this
# $query =
# "INSERT INTO cluster_status SET"
# . " cluster='$cluster'"
# . " ,queued=$queued"
# . " ,running=$running"
# . " ,status='$status'"
# . " ON DUPLICATE KEY UPDATE"
# . " queued=$queued"
# . " ,running=$running"
# . " ,status='$status'"
# . " ,time=CURRENT_TIMESTAMP()"
# ;
## without the primary key on gfac.cluster_status.cluster, we have to do two mysql calls
$query = "SELECT * FROM cluster_status WHERE cluster='$cluster'";
$result = mysqli_query( $gfac_link, $query );
if ( ! $result ) {
error_exit( "Query failed $query - " . mysqli_error( $gfac_link ) );
}
$rows = mysqli_num_rows( $result );
if ( $rows == 0 ) { ## INSERT
$query =
"INSERT INTO cluster_status SET"
. " cluster='$cluster'"
. " ,queued=$queued"
. " ,running=$running"
. " ,status='$status'"
;
} else { ## UPDATE
$query =
"UPDATE cluster_status SET"
. " queued=$queued"
. " ,running=$running"
. " ,status='$status'"
. " ,time=CURRENT_TIMESTAMP()"
. " WHERE cluster='$cluster'"
;
}
$result = mysqli_query( $gfac_link, $query );
if ( ! $result ) {
error_exit( "Query failed $query - " . mysqli_error( $gfac_link ) );
}
}
## Get local cluster status
function local_status() {
global $data;
global $cluster_configuration;
global $self;
foreach ( $cluster_configuration as $clname => $v ) {
if ( !isset( $v["active"] ) || $v["active"] != true ) {
continue;
}
if ( !isset( $v["status"] ) ) {
error_exit( "cluster $clname does not contain a status command" );
}
$results = run_cmd( $v["status"], false, true );
debug_json( "status for $clname", $results );
if ( count( $results ) != 3
|| !is_numeric( $results[1] )
|| !is_numeric( $results[2] )
) {
$sta = "down";
$run = 0;
$que = 0;
} else {
$sta = $results[ 0 ];
$run = $results[ 1 ];
$que = $results[ 2 ];
}
if ( $run != intval( $run ) || $que != intval( $que ) ) {
$sta = 'down';
$run = 0;
$que = 0;
}
## Save cluster status values
$a[ 'cluster' ] = $clname;
$a[ 'status' ] = $sta;
$a[ 'running' ] = $run;
$a[ 'queued' ] = $que;
$data[] = $a;
echo "$self: $clname $que $run $sta\n";
}
}