Thursday 18 April 2013

Solaris 10 - Service Management Facility

Most of the time i work on solaris machines and need to maintain services on these machines. But there are other users who uses these machines and by mistake they restart the services as root user. I've been thinking for a while on how to avoid this and also better way to manage services as it is tiresome job when you have to restart 50+ services manually by cdĂ­ng to the directory and run the startup script . Of course there are many auotmation tools in market and one i most like is puppet. But in this case i am not using puppet are any other third party automation tools but the solaris native SMF. In this post i've used the basic features of SMF.

To migrate the service to SMF, manifest file and method has to be created. The Manifest file is an xml and it's the key file where all the dependencies, method to be executed are defined.
Existing start procedure
1. cd /usr/local/test-service/
2. ls -ltr /usr/local/test-service/
-rwxr-xr-x   1 user1    group1    3646 Apr 18 06:25 user1agentctl
drwxr-xr-x   2 user1      group1     512 Apr 18 10:31 jars
-rw-r--r--   1 user1      group1          6 Apr 18 10:32 user1agent.pid
3. sux user1
4. ./user1agentctl -v start
5. Check if the process has started
[user1@test] $ cat user1agent.pid
22065
[user1@sjc3-vg1-java1] $ /usr/bin/ps -ef |grep 22065
user1    74 24627   0 10:36:54 pts/4       0:00 grep 22065
user1 22065     1   0 10:32:40 ?           0:01 javaXXXX -Xms256m -Xmx512m -Dapp.name=user1Processor

I need to do this 5 steps for every 50+ services, if the database goes down or the server crashed for some unkown reason. Using SMF simplified this work

Migrating service to SMF
Create the manifest file

[root@test] $ cat /var/svc/manifest/application/user1agent.xml


user1agent">
user1agent" type="service" version="1">
<create_default_instance enabled="false">

localhost/usr/local/test-service/user1agentctl"/>

svc/method/user1agent start" timeout_seconds="60">
<method_context>
<method_credential user="user1">

svc/method/user1agent stop" timeout_seconds="60">
<method_context>
<method_credential user="user1">


Create the method
[root@test] $ cat /lib/svc/method/user1agent
#!/sbin/sh
#
. /lib/svc/share/smf_include.sh
case "$1" in
start)
/usr/local/test-service/user1agentctl start
if [ $? -ne 0 ]
then
exit $SMF_EXIT_ERR_CONIG
fi
;;
stop)
/usr/local/test-service/user1agentctl stop
;;
*)
echo "Usage: $0 \c" >&2
echo "(start|stop)" >&2
exit 1
;;
esac
exit $SMF_EXIT_OK

Validate and import the manifest
[root@test] $ /usr/sbin/svccfg validate /var/svc/manifest/application/user1agent.xml
[root@test] $ /usr/sbin/svcadm restart manifest-import

Check if the service is added to the SMF
[root@test1] $ svcs -a |grep user1
disabled         10:32:24 svc:/application/user1agent:default

Let's do some testing

Enable the service SMF
/usr/sbin/svcadm enable svc:/application/user1agent:default
[root@test] $ svcs -a |grep user1
online         11:14:11 svc:/application/user1agent:default
root@test] $ /usr/bin/ps -ef |grep user1
user1 10600     1   1 11:14:12 ?           0:03 javaXXXX -Xms256m -Xmx512m -Dapp.name=user1Processor -classpath .::/usr/local/

Though i've enabled the service in SMF as root user, the process is running as user1. This is what i expected.

One more excellent feature is mentioned below

I will kill the process abruptly
[root@test] $ kill -9 10600                                                                                                                                           
[root@test] $ /usr/bin/ps -ef |grep user1
user1 18844     1   0 11:18:37 ?           0:06 javaXXXX -Xms256m -Xmx512m -Dapp.name=user1Processor -classpath .::/usr/local/jav

Even though i killed the process, the SMF has started it again.

These are other lot of features available in SMF, which i haven't explored yet.

Reference: http://bit.ly/13nE0Dw

No comments:

Post a Comment