Project

General

Profile

Download (6.53 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/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
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
function get_package_rcd_details($extd) {
51
	global $package_name, $executable_name, $description, $raw_name;
52
	$raw_name = str_replace(".sh","",$extd);
53
	$package_name = "";
54
	$executable_name = "";
55
	/* XXX: needs a get_pkg_description($packagename) function */
56
	$description = "";	
57
	$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
	/* if we cannot locate it atleast return what they passed us */
63
	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
}
71

    
72
if($_GET['service'] <> "") 
73
	get_package_rcd_details($_GET['service'] . ".sh");
74

    
75
if($_GET['restartservice'] == "true") {
76
	mwexec("/sbin/killall {$executable_name}");
77
	mwexec("/bin/sh /usr/local/etc/rc.d/{$raw_name}.sh start");
78
	$status = is_service_running($executable_name);
79
	if($status == 1) {
80
		$savemsg = "{$package_name} has been restarted.";
81
	} else {
82
		$error_message = exec_command("/bin/sh /usr/local/etc/rc.d/{$raw_name}.sh start");
83
		$savemsg = "There was a error restarting {$package_name}.<p>{$error_message}";
84
	}
85
}
86

    
87
if($_GET['stopservice'] == "true") {
88
	mwexec("/sbin/killall {$executable_name}");
89
	$status = is_service_running($executable_name);
90
	if($status == 1) {
91
		$savemsg = "There was an error stopping {$package_name} - {$executable_name}.";
92
	} else {
93
		$savemsg = "{$package_name} has been stopped.";
94
	}
95
}
96

    
97
if($_GET['startservice'] == "true") {	
98
	mwexec("/bin/sh /usr/local/etc/rc.d/{$raw_name}.sh start");
99
	$status = is_service_running($executable_name);
100
	if($status == 1) {
101
		$savemsg = "{$package_name} has been started.";
102
	} else {
103
		$error_message = exec_command("/bin/sh /usr/local/etc/rc.d/{$raw_name}.sh start");
104
		$savemsg = "There was a error restarting {$package_name}.<p>{$error_message}";
105
	}
106
}
107

    
108
/* batch mode, allow other scripts to call this script */
109
if($_GET['batch'] <> "") exit;
110

    
111
?>
112
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
113
<html>
114
<head>
115
<?php $title = "Status: Services"; ?>
116
<title><?=gentitle_pkg($title);?></title>
117
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
118
<link href="gui.css" rel="stylesheet" type="text/css">
119
</head>
120

    
121
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
122
<?php
123
include("fbegin.inc");
124
?>
125
<p class="pgtitle"><?=$title?></p>
126
<form action="status_services.php" method="post">
127
<?php if ($savemsg) print_info_box($savemsg); ?>
128

    
129
<p>
130

    
131

    
132
<table width="100%" border="0" cellpadding="0" cellspacing="0">
133
  <tr>
134
    <td class="tabcont">
135
    <table width="100%" border="0" cellpadding="6" cellspacing="0">
136
	<tr>
137
	  <td class="listhdrr"><b><center>Service</center></b></td>
138
	  <td class="listhdrr"><b><center>Description</center></b></td>
139
	  <td class="listhdrr"><b><center>Status</center></b></td>
140
	  <td class="listhdrr"><b><center>Maintenance</center></b></td>
141
	</tr>
142

    
143
<?php
144

    
145
$dh = @opendir("/usr/local/etc/rc.d/");
146
if ($dh)
147
	while (($extd = readdir($dh)) !== false) {
148
		if (($extd == ".") || ($extd == ".."))
149
			continue;
150
		get_package_rcd_details($extd);
151
		if($executable_name == "")
152
			continue;
153
		if($package_name == "")
154
			continue;
155
		if(get_pkg_id(strtolower($package_name)) == -1)
156
			continue;
157
		$status = is_service_running($executable_name);
158
		if($status == 1)
159
			$status_txt = "<font color='green'>Running</font>";
160
		else
161
			$status_txt = "<font color='red'>Stopped</font>";
162
		echo "<tr><td class=\"listlr\">{$package_name}</td>";
163
		echo "<td class=\"listlr\">{$description}</td>";
164
		echo "<td class=\"listlr\">{$status_txt}</td>";
165
		echo "<td class=\"listlr\"><center>";
166
		if($status == 1) {
167
			echo "<a href='status_services.php?restartservice=true&service={$raw_name}'>";
168
			echo "<img title='Restart Service' border='0' src='/service_restart.gif'></a> ";
169
			echo "<a href='status_services.php?stopservice=true&service={$raw_name}'>";
170
			echo "<img title='Stop Service' border='0' src='/service_stop.gif'> ";
171
			echo "</a> ";
172
		} else {
173
			echo "<a href='status_services.php?startservice=true&service={$raw_name}'> ";
174
			echo "<img title='Start Service' border='0' src='/service_start.gif'></a> ";
175
		}
176
		echo "</center></td>";
177
		echo "</tr>";
178
		$counter++;
179
	}
180

    
181
if($counter == 0) 
182
	echo "<tr><td colspan=5><center>Could not locate any services.</td></tr>";
183

    
184
?>
185
<tr><td>
186
</td></tr>
187
</table>
188

    
189
</td>
190
</tr></table>
191

    
192
<?php include("fend.inc"); ?>
193
</body>
194
</html>
(93-93/117)