status_services.php 7.1 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
3
/*
4
    Copyright (C) 2014-2015 Deciso B.V.
5 6
    Copyright (C) 2005-2006 Colin Smith <ethethlay@gmail.com>
    Copyright (C) 2004-2005 Scott Ullrich
Ad Schellevis's avatar
Ad Schellevis committed
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
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INClUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

require_once("guiconfig.inc");
32
require_once("services.inc");
33
require_once('plugins.inc');
34
require_once("vslb.inc");
35 36
require_once("system.inc");
require_once("unbound.inc");
37
require_once("pfsense-utils.inc");
38
require_once("openvpn.inc");
39
require_once("filter.inc");
40
require_once("ipsec.inc");
41
require_once("interfaces.inc");
42
require_once("rrd.inc");
Ad Schellevis's avatar
Ad Schellevis committed
43

44 45 46
if (!empty($_POST['service'])) {
    $service_name = $_POST['service'];
    switch ($_POST['action']) {
47
        case 'restart':
48
          echo service_control_restart($service_name, $_POST);
49
          break;
50
        case 'start':
51
          echo service_control_start($service_name, $_POST);
52
          break;
53
        case 'stop':
54
          echo service_control_stop($service_name, $_POST);
55 56
          break;
    }
57
    exit;
Ad Schellevis's avatar
Ad Schellevis committed
58 59
}

60 61
function service_control_start($name, $extras)
{
62 63
    $msg = sprintf(gettext('%s has been started.'), htmlspecialchars($name));

64
    if (!empty($extras['id'])) {
65
        $filter['id'] = $extras['id'];
66 67
    }

68
    $service = find_service_by_name($name, $filter);
69 70 71
    if (!isset($service['name'])) {
        return sprintf(gettext("Could not start unknown service `%s'"), htmlspecialchars($name));
    }
72
    if (isset($service['configd']['start'])) {
73 74 75
        foreach ($service['configd']['start'] as $cmd) {
            configd_run($cmd);
        }
76
    } elseif (isset($service['php']['start'])) {
77
        foreach ($service['php']['start'] as $cmd) {
78 79 80 81 82 83 84
            $params = array();
            if (isset($service['php']['args'])) {
                foreach ($service['php']['args'] as $param) {
                    $params[] = $service[$param];
                }
            }
            call_user_func_array($cmd, $params);
85
        }
86 87 88 89
    } elseif (isset($service['mwexec']['start'])) {
        foreach ($service['mwexec']['start'] as $cmd) {
            mwexec($cmd);
        }
90
    } else {
91
        $msg = sprintf(gettext("Could not start service `%s'"), htmlspecialchars($name));
92
    }
93

94
    return $msg;
95 96
}

97 98 99
function service_control_stop($name, $extras)
{
    $msg = sprintf(gettext("%s has been stopped."), htmlspecialchars($name));
100
    $filter = array();
101

102
    if (!empty($extras['id'])) {
103
        $filter['id'] = $extras['id'];
104 105 106
    }

    $service = find_service_by_name($name, $filter);
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    if (!isset($service['name'])) {
        return sprintf(gettext("Could not stop unknown service `%s'"), htmlspecialchars($name));
    }

    if (isset($service['configd']['stop'])) {
        foreach ($service['configd']['stop'] as $cmd) {
            configd_run($cmd);
        }
    } elseif (isset($service['php']['stop'])) {
        foreach ($service['php']['stop'] as $cmd) {
            $cmd();
        }
    } elseif (isset($service['mwexec']['stop'])) {
        foreach ($service['mwexec']['stop'] as $cmd) {
            mwexec($cmd);
        }
    } elseif (isset($service['pidfile'])) {
124
        killbypid($service['pidfile'], 'TERM', true);
125
    } else {
126 127
        /* last resort, but not very elegant */
        killbyname($service['name']);
128 129 130
    }

    return $msg;
131 132
}

133 134 135 136
function service_control_restart($name, $extras)
{
    $msg = sprintf(gettext("%s has been restarted."), htmlspecialchars($name));

137
    if (!empty($extras['id'])) {
138
        $filter['id'] = $extras['id'];
139
    }
140

141
    $service = find_service_by_name($name, $filter);
142 143 144 145 146 147 148 149 150 151
    if (!isset($service['name'])) {
        return sprintf(gettext("Could not restart unknown service `%s'"), htmlspecialchars($name));
    }

    if (isset($service['configd']['restart'])) {
        foreach ($service['configd']['restart'] as $cmd) {
            configd_run($cmd);
        }
    } elseif (isset($service['php']['restart'])) {
        foreach ($service['php']['restart'] as $cmd) {
152 153 154 155 156 157 158
            $params = array();
            if (isset($service['php']['args'])) {
                foreach ($service['php']['args'] as $param) {
                    $params[] = $service[$param];
                }
            }
            call_user_func_array($cmd, $params);
159 160 161 162 163 164 165 166 167 168
        }
    } elseif (isset($service['mwexec']['restart'])) {
        foreach ($service['mwexec']['restart'] as $cmd) {
            mwexec($cmd);
        }
    } else {
        $msg = sprintf(gettext("Could not restart service `%s'"), htmlspecialchars($name));
    }

    return $msg;
169 170
}

171
$services = services_get();
172 173 174

if (count($services) > 0) {
    uasort($services, "service_name_compare");
175 176
}

Ad Schellevis's avatar
Ad Schellevis committed
177 178 179 180
include("head.inc");

?>

181
<body>
182

183
<?php include("fbegin.inc"); ?>
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
  <section class="page-content-main">
    <div class="container-fluid">
      <div class="row">
        <?php if (isset($savemsg)) print_info_box($savemsg); ?>
        <section class="col-xs-12">
          <div class="content-box">
            <div class="table-responsive">
              <table class="table table-striped">
              <thead>
                <tr>
                  <td><?=gettext("Service");?></td>
                  <td><?=gettext("Description");?></td>
                  <td><?=gettext("Status");?></td>
                </tr>
              </thead>
              <tbody>
<?php
               if (count($services) > 0):
                foreach($services as $service):?>
                <tr>
                    <td><?=$service['name'];?></td>
                    <td><?=$service['description'];?></td>
                    <td>
                      <?=get_service_status_icon($service, true, true);?>
208
                      <?=get_service_control_links($service, false);?>
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
                    </td>
                </tr>
<?php
                endforeach;
              else:?>
                <tr>
                  <td colspan="3"> <?=gettext("No services found");?></td>
                </tr>
<?php
                 endif;?>
              </tbody>
            </table>
          </div>
        </div>
      </section>
    </div>
  </div>
</section>
227
<?php include("foot.inc"); ?>