Showing posts with label service. Show all posts
Showing posts with label service. Show all posts

Friday, June 13, 2008

Using NTSYSV To Manage Linux Services

Hey There,

Picking up where we left off in yesterday's post regarding using chkconfig to manage Linux run levels, today we're going to take a look at another common, and helpful, tool you can find on Redhat, Fedora and other such similar Linux distro's, named ntsysv. Actually, we'll be looking at two commands, since there's a slight dependency (which we'll get to befoe this post is over) between ntsysv and another command named service.

Click on the image below to see it in its unaltered state ;)

The intuitive user interface for ntsysv

ntsysv is a great tool to use if you like GUI-based interaction and aren't able to run an X-Windows session. Since it's basically a tty GUI (Sometimes referred to as a TUI), you can enjoy the graphical interaction through any terminal software that supports basic color in the shell.

You may also have noted, in the past, that the menu we created in our post on creating a simple menu using tput and ANSI colors looks eerily similar. This is only true at the base level, as our TUI menu was much more limited. Check out either of the above links for pictorial depictions of all the various very-portable colors you can use in the shell and some good information on how to make use of color in your Linux shell.

Back to ntsysv :) This command is very simple to use and can be invoked in only a few ways. The most common is without arguments, like this:

host # ntsysv

This will bring up the TUI, which should look roughly like the image near the top of this post. The contents of the ntsysv interface will probably differ, since it's populated with the names of packages (or services) that you have installed on your system and that it is able to control. The interface is very simple to use and the interaction basically consists of your either checking, or unchecking, the box next to the service name on any given line. When you exit ntsysv (by selecting OK or Cancel), all the services that have their checkboxes checked, will be considered active, and the unchecked services will be rendered inactive (see below for a small "gotcha" concerning ntsysv's specific behaviour in this matter).

By default, when you invoke ntsysv with no command line arguments, you'll be editing only the run level at which you invoke it. So, if your system is at run level 3 and you just run ntsysv, you'll only be editing the on/off functionality of services at run level 3. If you want to be more specific, and make changes across multiple run levels, you can specify that at the command line when you invoke the program, like so:

host # ntsysv --level 2345

The above, for instance, would bring up the same interface, but you'd be editing run levels 2, 3, 4 and 5 all at the same time. The other nice thing about this command is that, unlike using chkconfig to manage run levels you'll just be using your space bar (or mouse, depending on how you have your setup set up ;) to check and uncheck boxes, rather than typing on or off on a per-service basis. A workaround to that is posted in yesterday's write up on chkconfig, but ntsysv has that functionality bundled in. On the flipside of the coin, you can't use ntsysv to add new services. You only get to work with what the TUI shows you.

Some versions of ntsysv also have a --back flag that you can use to modify the look and feel of the Cancel button (in other words, it will change the text on the Cancel button to Back). Useful? You be the judge ;)

host # ntsysv --back <-- Note that this can also be used in conjunction with the --level flag.

And, for that "gotcha" I promised to deliver way back near the top of this post ;) This is something to keep in mind if you need changes to be enacted "immediately." ntsysv's default behaviour is to automatically start (or stop) a service, after you've select/unselected it in the TUI and entered OK. But, this automatic starting and stopping of services only works for services managed by xinetd.

If you, for instance, wanted to stop any service not managed by xinetd (which you can determine by looking for it in the /etc/xinetd.d directory), you would need to follow up your ntsysv session by stopping the service with the "service" command, like so:

host # service SERVICENAME stop

Of course, if you want to start it up, you'd use "start" in place of the "stop" directive (other options for the "service" command include restart, status, etc, and can be highly specific to the service itself since those directives are derived from the specific service's init script). If you don't do this, for services not managed by xinetd, your changes won't take place until run levels change in the appropriate manner (like during a reboot, for instance).

For those of you who've never used ntsysv, I'd suggest giving it a try. While it is limited, it's a great tool for getting tedious work done simply and can also be a great way to visually check on the status of services at any run level.

Enjoy :)

, Mike

Wednesday, January 2, 2008

A Simple CGI Script To Check Network Hosts' Status

Today, I thought I'd put out another script that might be helpful to at least a few of us out there. As you may recall, in an earlier post we went over a script that checked network hosts' ports non-maliciously. This is a combination of that with a little something extra.

Okay, there are a few extra elements here. The main function of the script is to check that all the servers in the @hostnames array are up and running. This is done as simply as possible, using ping. The second thing it checks is that FTP is up and running on all hosts (We're assuming that you're running this from a box on your network that has access - through firewalls, router acl's and whatnot - to all of the servers listed). Thirdly, it looks at a subset of servers that you define within the script, and tries to determine if HTTP is up and running.

This script is slightly more complicated than the other, as it uses the Perl Socket and CGI modules and also makes use of signal handling (Touched on in more detail in this previous post. Finally, we do some while loop tagging to control the flow.

Of course, this script can be modified easily to include other system checks. I wrote this to get a quick feel for the health of the network and get an easy read on any problems at whatever time I chose to open the page in my web browser.

Hopefully, you'll find this useful. I've commented the script near all the parts that can/should be edited (IN CAPITALS FOR EASE OF VIEWING). Otherwise, it's pretty much self explanatory.

Cheers,


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/usr/bin/perl

#
# whatup.cgi - 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

use CGI;
use Socket;
require 5.002;

# FILL THIS ARRAY WITH ALL THE HOSTNAMES YOU WANT TO MONITOR

@hostnames = qw(host1 host2 host3 hosta hostb hostc hostn);

foreach $pingable_host (@hostnames) {
$answer=`/usr/sbin/ping $pingable_host 1 1`;
if ( $answer=~ /alive/ ) {
$is_it_up_or_not[$a] = 1;
$a++;
} else {
$is_it_up_or_not[$a] = 0;
$a++;
}
}

use CGI;

$whatup_cgi = new CGI;
@ports = qw(21 80);
$date = `date`;

print $whatup_cgi->header();
print "<html><head><meta HTTP-EQUIV=\"REFRESH\" CONTENT=\"300; URL=/whatup.cgi\"><title>What's Up?</title></head>\n";
print "<body BGCOLOR=\"#000000\" TEXT=\"#00FF00\" LINK=\"#FFFF00\" VLINK=\"#FF0000\" ALINK=\"#BECD8C\">\n";
print "<p>\n";
print "<br><hr><br>\n";
print "<p>\n";
print "<center><FONT COLOR=\"#FF0000\"><H3>Your Immediate Guide to What's Up On Our Network!</h3></FONT></center>\n";
print "<center><FONT COLOR=\"#BECD8C\"><H4>$date</h4></FONT></center>\n";
print "<p>\n";
print "<br><hr><br>\n";
print "<p>\n";

$b=0;
$c=0;

$SIG{'ALRM'} = 'next_goround';
OUTER: foreach $host (@hostnames) {
if ( $is_it_up_or_not[$b] ) {
print "<hr>\n";
print "<h3><font color=\"#BECD8C\">$host is Up!</font></h3>";
} else {
print "<hr>\n";
print "<h3><font color=\"#FF0000\">$host is Down!</font></h3>";
}
print "<ul>";
$CONNECTION_PORT = "NEXT_UP_IS${c}SOCK";

# IN THE BELOW IF STATEMENT, USE REGULAR EXPRESSIONS TO MATCH ALL HOST NAMES
# THAT YOU WANT TO BE CHECKED FOR HTTP. ALL HOSTS ARE CHECKED FOR FTP

if ( $host=~ /host[1-3]/ || $host=~ /host[ab]/ ) {
foreach $portno (@ports) {
$port = $portno;
if ($port =~ /\D/) {
$port = getservbyname($port, 'tcp');
}
$iaddr = inet_aton($host);
$paddr = sockaddr_in($port, $iaddr);
$proto = getprotobyname('tcp');
socket($CONNECTION_PORT, PF_INET, SOCK_STREAM, $proto);
if ( $is_it_up_or_not[$b] ) {
connect($CONNECTION_PORT, $paddr) unless alarm 10;
if ( $success_or_failure ) {
next;
}
alarm 0;
}
if ($port == 21 && ! $success_or_failure ) {
$portname = "FTP";
if ( $is_it_up_or_not[$b] ) {
$create_socket = send($CONNECTION_PORT, "user anonymous username\@xyz.com", 40) unless alarm 10;
alarm 0;
}
} else {
$portname = "HTTP";
if ( $is_it_up_or_not[$b] ) {
$create_socket = send($CONNECTION_PORT, "GET /index.html HTTP/1.0", 40) unless alarm 10;
alarm 0;
}
}
close($CONNECTION_PORT);
if ( ! $is_it_up_or_not[$b] && ! $success_or_failure ) {
print "<li><h3><font color=\"#FF0000\">$portname is Down!</h3></font></ul>";
$b++;
next OUTER;
}
if ($create_socket) {
print "<li><h3><font color=\#FFFF00\">$portname is Up!</h3></font>";
} else {
print "<li><h3><font color=\"#FF0000\">$portname is Down!</h3></font>";
}
$c++;
$success_or_failure = 0;
}
print "</ul>\n";
} else {
$port = 21;
if ($port =~ /\D/) {
$port = getservbyname($port, 'tcp');
}
$iaddr = inet_aton($host);
$paddr = sockaddr_in($port, $iaddr);
$proto = getprotobyname('tcp');
socket($CONNECTION_PORT, PF_INET, SOCK_STREAM, $proto);
if ( $is_it_up_or_not[$b] ) {
connect($CONNECTION_PORT, $paddr) unless alarm 10;
alarm 0;
}
if ( $is_it_up_or_not[$b] && ! $success_or_failure ) {
$create_socket = send($CONNECTION_PORT, "user anonymous username\@xyz.com", 40) unless alarm 10;
alarm 0;
}
$portname = "FTP";
close($CONNECTION_PORT);
if ( ! $is_it_up_or_not[$b] && ! $success_or_failure ) {
print "<li><h3><font color=\"#FF0000\">$portname is Down!</h3></font></ul>";
$b++;
next OUTER;
}
if ($create_socket) {
print "<li><h3><FONT COLOR=\"#FFFF00\">$portname is Up!</h3></font>";
} else {
print "<li><h3><font color=\"#FF0000\">$portname is Down!</h3></font>";
}
print "</ul>\n";
$c++;
$success_or_failure = 0;
}
$b++;
}

$whatup_cgi->end_html();

sub next_goround {

close($CONNECTION_PORT);
return $success_or_failure = 1;
}


, Mike