dashboard.ajax 15 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2000-2018 University of Utah and the Flux Group.
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
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#
chdir("..");
chdir("apt");
include_once("instance_defs.php");

#
# Number of experiments started over range of time periods. 
#
$scales  = array("Day"   => (24 * 3600),
                 "Week"  => (24 * 3600 * 7),
                 "Month" => (24 * 3600 * 30),
                 "Year"  => (24 * 3600 * 365),
                 "Epoch" => time());

#
# Server side of getting dashboard stats.
#
function Do_GetStats()
{
42
    global $scales, $geni_response_codes, $urn_mapping, $TBBASE;
43 44 45 46 47

    if (! (ISADMIN() || ISFOREIGN_ADMIN())) {
        SPITAJAX_ERROR(1, "Not enough permission");
        return;
    }
48
    
49 50 51 52 53 54
    $started   = array();
    $errors    = array();
    $breakdown = array();
    $breakdown["timedout"] = array();
    $breakdown["mapping"]  = array();
    $breakdown["other"]    = array();
55 56 57 58 59

    foreach ($scales as $key => $age) {
        $query_result =
            DBQueryFatal("select ".
                 "(select count(uuid) from apt_instances ".
60
                 " where started > DATE_SUB(now(),interval $age second)),".
61
                 "(select count(uuid) from apt_instance_history ".
62
                 " where started > DATE_SUB(now(),interval $age second))");
63 64
        $row = mysql_fetch_array($query_result);
        $started[$key] = $row[0] + $row[1];
65 66 67

        $query_result =
            DBQueryFatal("select count(uuid) from apt_instance_failures ".
68
                    " where started > DATE_SUB(now(),interval $age second)");
69 70
        $row = mysql_fetch_array($query_result);
        $errors[$key] = $row[0] + $row[1];
71

72 73 74 75
        $breakdown["timedout"][$key] = 0;
        $breakdown["mapping"][$key]  = 0;
        $breakdown["other"][$key]    = 0;

76 77 78
        $query_result =
            DBQueryFatal("select exitcode,count(exitcode) as count ".
                         "  from apt_instance_failures ".
79
                         "where started>DATE_SUB(now(),interval $age second)".
80 81 82 83
                         "group by exitcode");
        while ($row = mysql_fetch_array($query_result)) {
            $exitcode = $row[0];
            $count    = $row[1];
84
            if ($exitcode == GENIRESPONSE_TIMEDOUT) {
85 86
                $breakdown["timedout"][$key] = $count;
            }
87 88 89
            elseif ($exitcode == GENIRESPONSE_BADARGS ||
                    ($exitcode >= GENIRESPONSE_VLAN_UNAVAILABLE &&
                     $exitcode <= GENIRESPONSE_NO_MAPPING)) {
90 91 92 93 94 95
                $breakdown["mapping"][$key] += $count;
            }
            else {
                $breakdown["other"][$key] += $count;
            }
        }
96
    }
97 98 99
    $dashboard = array("started"   => $started,
                       "errors"    => $errors,
                       "breakdown" => $breakdown);
100 101 102 103 104 105 106

    #
    # What are most recent swapins
    #
    $latest = array();

    $query_result =
107
        DBQueryFatal("select i.name,i.uuid,i.creator,i.creator_idx,".
108
                     "  i.physnode_count,i.started,i.aggregate_urn, ".
109 110 111 112
                     "  p.uuid as profile_uuid,p.name as profile_name ".
                     "from apt_instances as i ".
                     "left join apt_profiles as p on ".
                     "     p.profileid=i.profile_id ".
113
                     "order by started desc limit 5");
114 115 116 117 118 119
    while ($row = mysql_fetch_array($query_result)) {
        $name        = $row["name"];
        $uuid        = $row["uuid"];
        $creator     = $row["creator"];
        $creator_idx = $row["creator_idx"];
        $pcount      = $row["physnode_count"];
120
        $created     = DateStringGMT($row["started"]);
121 122 123 124
        $profile_uuid = $row["profile_uuid"];
        $profile_name = $row["profile_name"];
        $aggregate_urn= $row["aggregate_urn"];
        $cluster      = "N/A";
125

126 127 128 129 130
        if ($aggregate_urn && $aggregate_urn != "") {
            $cluster = $urn_mapping[$aggregate_urn];
        }
        $latest[$uuid] = array("uuid" => $uuid,
                               "name" => $name,
131 132 133
                               "creator_idx" => $creator_idx,
                               "creator" => $creator,
                               "physnode_count" => $pcount,
134 135 136 137
                               "created" => $created,
                               "cluster"  => $cluster,
                               "profile_uuid" => $profile_uuid,
                               "profile_name" => $profile_name);
138 139 140 141 142 143 144 145 146 147 148
    }
    $dashboard["latest"] = $latest;

    #
    # Who are the heaviest users
    #
    $users    = array();

    $query_result =
        DBQueryFatal("select creator,creator_idx,count(creator) as ecount, ".
                     "   sum(physnode_count) as pcount ".
Leigh Stoller's avatar
Leigh Stoller committed
149
                     "from apt_instances group by creator,creator_idx ".
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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
                     "order by pcount desc limit 10");
    while ($row = mysql_fetch_array($query_result)) {
        $creator     = $row["creator"];
        $creator_idx = $row["creator_idx"];
        $pcount      = $row["pcount"];
        $ecount      = $row["ecount"];

        $users[$creator] = array("creator_idx" => $creator_idx,
                                 "physnode_count" => $pcount,
                                 "experiment_count" => $ecount);
    }
    $dashboard["heaviest_users"] = $users;

    #
    # Who are the heaviest projects
    #
    $projects = array();

    $query_result =
        DBQueryFatal("select pid,count(pid) as ecount, ".
                     "   sum(physnode_count) as pcount ".
                     "from apt_instances group by pid ".
                     "order by pcount desc limit 10");
    while ($row = mysql_fetch_array($query_result)) {
        $pid     = $row["pid"];
        $pcount  = $row["pcount"];
        $ecount  = $row["ecount"];

        $projects[$pid] = array("pid" => $pid,
                                "physnode_count" => $pcount,
                                "experiment_count" => $ecount);
    }
    $dashboard["heaviest_projects"] = $projects;

    #
    # Most popular profiles (since the Epoch).
    #
    $profiles = array();

    $query_result =
        DBQueryFatal("select p.uuid,p.name,count(h.profile_id) as count ".
                     "   from apt_instance_history as h ".
                     "left join apt_profiles as p on p.profileid=h.profile_id ".
                     "group by profile_id order by count desc limit 10");
    while ($row = mysql_fetch_array($query_result)) {
        $name       = $row["name"];
        $count      = $row["count"];
        $uuid       = $row["uuid"];

        $profiles[$name] = array("name"  => $name,
                                 "uuid"  => $uuid,
                                 "count" => $count);
    }
    $dashboard["heaviest_profiles"] = $profiles;

205 206 207 208 209 210
    #
    # Detailed info on the 10 most recent errors.
    #
    $error_details = array();

    $query_result =
211
        DBQueryFatal("select f.name,exitcode,f.started,f.public_url,f.uuid,".
212 213
                     "  IF(i.uuid,1,0) as active,f.exitmessage, ".
                     "  p.uuid as profile_uuid,p.name as profile_name, ".
214 215 216
                     "  IF(i.uuid,i.aggregate_urn,h.aggregate_urn) ".
                     "    as aggregate_urn, ".
                     "  f.slice_uuid,f.logfileid,f.creator ".
217 218 219 220 221
                     "from apt_instance_failures as f ".
                     "left join apt_instances as i on ".
                     "     i.uuid=f.uuid ".
                     "left join apt_instance_history as h on ".
                     "     h.uuid=f.uuid ".
222 223
                     "left join apt_profiles as p on ".
                     "     p.profileid=f.profile_id ".
224
                     "order by f.started desc limit 10");
225 226 227 228 229

    while ($row = mysql_fetch_array($query_result)) {
        $name       = $row["name"];
        $exitcode   = $row["exitcode"];
        $url        = $row["public_url"];
230
        $logfileid  = $row["logfileid"];
231 232
        $active     = $row["active"] ? 1 : 0;
        $uuid       = $row["uuid"];
233
        $created    = DateStringGMT($row["started"]);
234
        $creator    = $row["creator"];
235 236
        $message    = $row["exitmessage"];
        $reason     = $exitcode;
237
        $slice_uuid = $row["slice_uuid"];
238 239 240 241
        $profile_uuid = $row["profile_uuid"];
        $profile_name = $row["profile_name"];
        $aggregate_urn= $row["aggregate_urn"];
        $cluster      = "N/A";
242 243 244 245

        if ($exitcode >= 0 && $exitcode <= count($geni_response_codes)) {
            $reason = $geni_response_codes[$exitcode];
        }
246 247 248
        elseif ($exitcode == GENIRESPONSE_STITCHER_ERROR) {
            $reason = "Stitcher Failed";
        }
249 250 251
        if ($aggregate_urn && $aggregate_urn != "") {
            $cluster = $urn_mapping[$aggregate_urn];
        }
252 253 254 255 256 257 258 259 260
        #
        # Ick. If the url is for the showslicepub link, we have a problem;
        # the link is good only while the slice is alive. After that, we have
        # to use a private url (which is okay, cause only admins are going
        # to see it and be able to use it).
        #
        if (ISFOREIGN_ADMIN()) {
            $url = null;
        }
261 262 263 264 265 266 267 268 269
        elseif (!$url) {
            #
            # If there is a log file, show that instead.
            #
            if ($logfileid) {
                $url = $TBBASE . "/" .
                    CreateURL("spewlogfile", "logfile", $logfileid);
            }
        }
270 271 272 273 274 275 276 277 278
        elseif (preg_match("/^(https:.*)\/showslicepub/", $url, $matches)) {
            if (ISADMIN()) {
                $url = $matches[1] . "/showslicelogs.php?slice_uuid=".
                    $slice_uuid;
            }
            else {
                $url = null;
            }
        }
279 280 281 282 283
        $error_details[$uuid] = array("name"     => $name,
                                      "uuid"     => $uuid,
                                      "exitcode" => $exitcode,
                                      "active"   => $active,
                                      "created"  => $created,
284
                                      "creator"  => $creator,
285 286
                                      "reason"   => $reason,
                                      "message"  => $message,
287 288 289 290 291
                                      "url"      => $url,
                                      "cluster"  => $cluster,
                                      "profile_uuid" => $profile_uuid,
                                      "profile_name" => $profile_name);
                                      
292 293 294
    }
    $dashboard["error_details"] = $error_details;
    
295 296 297 298 299 300 301 302 303 304 305 306
    #
    # Experiments waiting for extensions from an administrator
    #
    $extension_details = array();

    $query_result =
        DBQueryFatal("select a.*,s.expires, ".
                     "   (UNIX_TIMESTAMP(now()) > ".
                     "    UNIX_TIMESTAMP(s.expires)) as expired ".
                     "  from apt_instances as a ".
                     "left join geni.geni_slices as s on ".
                     "     s.uuid=a.slice_uuid ".
307
                     "where a.extension_requested=1 order by s.expires asc");
308 309 310 311 312

    while ($row = mysql_fetch_array($query_result)) {
        $name       = $row["name"];
        $uuid       = $row["uuid"];
        $pid        = $row["pid"];
313
        $expires    = DateStringGMT($row["expires"]);
314
        $created    = DateStringGMT($row["started"]);
315 316 317 318 319 320 321 322 323 324
        $creator    = $row["creator"];

        $extension_details[$uuid] = array("name"     => $name,
                                          "uuid"     => $uuid,
                                          "pid"      => $pid,
                                          "creator"  => $creator,
                                          "created"  => $created,
                                          "expires"  => $expires);
    }
    $dashboard["extension_requests"] = $extension_details;
325 326 327 328 329 330 331 332

    #
    # List of all locked down experiments.
    #
    $lockdown = array();

    $query_result =
        DBQueryFatal("select i.name,i.uuid,i.creator,i.creator_idx,".
333
                     "  i.physnode_count,i.started,i.aggregate_urn,s.expires, ".
334 335 336 337 338 339 340
                     "  p.uuid as profile_uuid,p.name as profile_name ".
                     "from apt_instances as i ".
                     "left join apt_profiles as p on ".
                     "     p.profileid=i.profile_id ".
                     "left join geni.geni_slices as s on ".
                     "     s.uuid=i.slice_uuid ".
                     "where i.admin_lockdown!=0 ".
341
                     "order by i.started desc");
342 343 344 345 346 347
    while ($row = mysql_fetch_array($query_result)) {
        $name        = $row["name"];
        $uuid        = $row["uuid"];
        $creator     = $row["creator"];
        $creator_idx = $row["creator_idx"];
        $pcount      = $row["physnode_count"];
348
        $created     = DateStringGMT($row["started"]);
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
        $expires     = DateStringGMT($row["expires"]);
        $profile_uuid = $row["profile_uuid"];
        $profile_name = $row["profile_name"];
        $aggregate_urn= $row["aggregate_urn"];
        $cluster      = "N/A";

        if ($aggregate_urn && $aggregate_urn != "") {
            $cluster = $urn_mapping[$aggregate_urn];
        }
        $lockdown[$uuid] = array("uuid" => $uuid,
                               "name" => $name,
                               "creator_idx" => $creator_idx,
                               "creator" => $creator,
                               "physnode_count" => $pcount,
                               "created" => $created,
                               "expires" => $expires,
                               "cluster"  => $cluster,
                               "profile_uuid" => $profile_uuid,
                               "profile_name" => $profile_name);
    }
    $dashboard["lockdown"] = $lockdown;

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
    #
    # Grab the node type counts for all clusters
    #
    $typecounts = array();
    
    $query_result =
        DBQueryFatal("select nickname,type,count,free ".
                     "   from apt_aggregate_nodetypes as t ".
                     "left join apt_aggregates as a on a.urn=t.urn");
    
    while ($row = mysql_fetch_array($query_result)) {
        $site = $row["nickname"];
        $type = $row["type"];
        
        $blob = array("site"  => $site,
                      "type"  => $type,
                      "count" => $row["count"],
                      "free"  => $row["free"]);
        if (!array_key_exists($site, $typecounts)) {
            $typecounts[$site] = array();
        }
        $typecounts[$site][$type] = $blob;
    }
    $dashboard["typecounts"] = $typecounts;

396 397 398 399 400 401 402
    SPITAJAX_RESPONSE($dashboard);
}

# Local Variables:
# mode:php
# End:
?>