Project

General

Profile

Download (6.29 KB) Statistics
| Branch: | Tag: | Revision:
1 a0e4cea2 Scott Ullrich
#!/usr/local/bin/php
2
<?php
3
/*
4
    services_status.php
5
    Copyright (C) 2004, 2005 Scott Ullrich
6
    All rights reserved.
7
8
    Redistribution and use in source and binary forms, with or without
9
    modification, are permitted provided that the following conditions are met:
10
11
    1. Redistributions of source code must retain the above copyright notice,
12
       this list of conditions and the following disclaimer.
13
14
    2. Redistributions in binary form must reproduce the above copyright
15
       notice, this list of conditions and the following disclaimer in the
16
       documentation and/or other materials provided with the distribution.
17
18
    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
22
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
    POSSIBILITY OF SUCH DAMAGE.
28
*/
29
30
require("guiconfig.inc");
31
require("xmlparse_pkg.inc");
32
33
function gentitle_pkg($pgname) {
34
	global $config;
35
	return $config['system']['hostname'] . "." . $config['system']['domain'] . " - " . $pgname;
36
}
37
38 57e15da9 Scott Ullrich
function find_package_description($package) {
39
    global $g, $config;
40
    if(!file_exists("{$g['tmp_path']}/pkg_config.xml"))
41
	    fetch_latest_pkg_config();
42
    $pkg_config = parse_xml_config_pkg("{$g['tmp_path']}/pkg_config.xml", "pfsensepkgs");
43
    foreach($pkg_config['packages']['package'] as $index) {
44
	if($index['name'] == $package) 
45
	    return $index['descr'];
46
    }
47
    return;
48
}
49
50 a0e4cea2 Scott Ullrich
function get_package_rcd_details($extd) {
51 b694936e Scott Ullrich
	global $package_name, $executable_name, $description, $raw_name;
52 a9bc3b82 Scott Ullrich
	$raw_name = str_replace(".sh","",$extd);
53
	$package_name = "";
54
	$executable_name = "";
55
	/* XXX: needs a get_pkg_description($packagename) function */
56
	$description = "";	
57 a0e4cea2 Scott Ullrich
	$file_contents = return_filename_as_string("/usr/local/etc/rc.d/{$extd}");
58
        if (preg_match_all("/\# PACKAGE\: (.*)\n/",$file_contents,$match_array))
59
            $package_name = $match_array[1][0];
60
        if (preg_match_all("/\# EXECUTABLE\: (.*)\n/",$file_contents,$match_array))
61
            $executable_name = $match_array[1][0];
62 255b0b7d Scott Ullrich
	/* if we cannot locate it atleast return what they passed us */
63 a9bc3b82 Scott Ullrich
	if($package_name == "") 
64
		$package_name = str_replace(".sh","",$extd);
65
	if($executable_name == "") 
66
		$executable_name = str_replace(".sh","",$extd);
67
	$description = find_package_description($raw_name);
68
	if($description == "")
69
		$description = "&nbsp;";
70 a0e4cea2 Scott Ullrich
}
71
72 b694936e Scott Ullrich
if($_GET['service'] <> "") 
73 a0e4cea2 Scott Ullrich
	get_package_rcd_details($_GET['service'] . ".sh");
74
75
if($_GET['restartservice'] == "true") {
76
	mwexec("/sbin/killall $executable_name");
77 4fd3d5ec Scott Ullrich
	mwexec("/bin/sh /usr/local/etc/rc.d/{$raw_name}.sh start");
78 eb90233d Scott Ullrich
	$status = is_service_running($executable_name);
79 3d13f780 Scott Ullrich
	if($status == 1) {
80 eb90233d Scott Ullrich
		$savemsg = "{$package_name} has been restarted.";
81 3d13f780 Scott Ullrich
	} else {
82 b694936e Scott Ullrich
		$error_message = exec_command("/bin/sh /usr/local/etc/rc.d/{$raw_name}.sh");
83 3d13f780 Scott Ullrich
		$savemsg = "There was a error restarting {$package_name}.<p>{$error_message}";
84
	}
85 a0e4cea2 Scott Ullrich
}
86
87
if($_GET['stopservice'] == "true") {
88
	mwexec("/sbin/killall $executable_name");
89
	$savemsg = "{$package_name} has been stopped.";
90
}
91
92
if($_GET['startservice'] == "true") {	
93 4fd3d5ec Scott Ullrich
	mwexec("/bin/sh /usr/local/etc/rc.d/{$raw_name}.sh start");
94 3d13f780 Scott Ullrich
	if($status == 1) {
95 eb90233d Scott Ullrich
		$savemsg = "{$package_name} has been started.";
96 3d13f780 Scott Ullrich
	} else {
97 b694936e Scott Ullrich
		$error_message = exec_command("/bin/sh /usr/local/etc/rc.d/{$raw_name}.sh");
98 3d13f780 Scott Ullrich
		$savemsg = "There was a error restarting {$package_name}.<p>{$error_message}";
99
	}
100 eb90233d Scott Ullrich
101 a0e4cea2 Scott Ullrich
}
102
103
/* batch mode, allow other scripts to call this script */
104
if($_GET['batch'] <> "") exit;
105
106
?>
107
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
108
<html>
109
<head>
110
<?php $title = "Status: Services"; ?>
111
<title><?=gentitle_pkg($title);?></title>
112
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
113
<link href="gui.css" rel="stylesheet" type="text/css">
114
</head>
115
116
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
117
<?php
118
include("fbegin.inc");
119
?>
120
<p class="pgtitle"><?=$title?></p>
121
<form action="status_services.php" method="post">
122
<?php if ($savemsg) print_info_box($savemsg); ?>
123
124
<p>
125
126 a9bc3b82 Scott Ullrich
127
<table width="100%" border="0" cellpadding="0" cellspacing="0">
128
  <tr>
129
    <td class="tabcont">
130
    <table width="100%" border="0" cellpadding="6" cellspacing="0">
131
	<tr>
132
	  <td class="listhdrr"><b><center>Service</center></b></td>
133
	  <td class="listhdrr"><b><center>Description</center></b></td>
134
	  <td class="listhdrr"><b><center>Status</center></b></td>
135
	  <td class="listhdrr"><b><center>Maintenance</center></b></td>
136
	</tr>
137
138 a0e4cea2 Scott Ullrich
<?php
139
140
$dh = @opendir("/usr/local/etc/rc.d/");
141
if ($dh)
142
	while (($extd = readdir($dh)) !== false) {
143 a9bc3b82 Scott Ullrich
		if (($extd == ".") || ($extd == ".."))
144 a0e4cea2 Scott Ullrich
			continue;
145
		get_package_rcd_details($extd);
146 a9bc3b82 Scott Ullrich
		if($executable_name == "")
147
			continue;
148
		if($package_name == "")
149 a0e4cea2 Scott Ullrich
			continue;
150 fa903e29 Scott Ullrich
		if(get_pkg_id($package_name) == -1)
151 230ede67 Scott Ullrich
			continue;
152 a0e4cea2 Scott Ullrich
		$status = is_service_running($executable_name);
153
		if($status == 1)
154 5c6bef5b Scott Ullrich
			$status_txt = "<font color='green'>Running</font>";
155 a0e4cea2 Scott Ullrich
		else
156 5c6bef5b Scott Ullrich
			$status_txt = "<font color='red'>Stopped</font>";
157 a9bc3b82 Scott Ullrich
		echo "<tr><td class=\"listlr\">{$package_name}</td>";
158
		echo "<td class=\"listlr\">{$description}</td>";
159
		echo "<td class=\"listlr\">{$status_txt}</td>";
160 c51904c7 Scott Ullrich
		echo "<td class=\"listlr\"><center>";
161 a0e4cea2 Scott Ullrich
		if($status == 1) {
162 b694936e Scott Ullrich
			echo "<a href='status_services.php?restartservice=true&service={$raw_name}'>";
163 221054b5 Scott Ullrich
			echo "<img title='Restart Service' border='0' src='/service_restart.gif'></a> ";
164 b694936e Scott Ullrich
			echo "<a href='status_services.php?stopservice=true&service={$raw_name}'>";
165 221054b5 Scott Ullrich
			echo "<img title='Stop Service' border='0' src='/service_stop.gif'> ";
166 57e15da9 Scott Ullrich
			echo "</a> ";
167 a0e4cea2 Scott Ullrich
		} else {
168 b694936e Scott Ullrich
			echo "<a href='status_services.php?startservice=true&service={$raw_name}'> ";
169 221054b5 Scott Ullrich
			echo "<img title='Start Service' border='0' src='/service_start.gif'></a> ";
170 a0e4cea2 Scott Ullrich
		}
171 c51904c7 Scott Ullrich
		echo "</center></td>";
172 a0e4cea2 Scott Ullrich
		echo "</tr>";
173 907e49b8 Scott Ullrich
		$counter++;
174 a0e4cea2 Scott Ullrich
	}
175
176 907e49b8 Scott Ullrich
if($counter == 0) 
177
	echo "<tr><td colspan=5><center>Could not locate any services.</td></tr>";
178
179 a0e4cea2 Scott Ullrich
?>
180
<tr><td>
181
</td></tr>
182
</table>
183
184 a9bc3b82 Scott Ullrich
</td>
185
</tr></table>
186
187 a0e4cea2 Scott Ullrich
<?php include("fend.inc"); ?>
188
</body>
189
</html>