5 # The contents of this file are subject to the terms of the
6 # Common Development and Distribution License (the "License").
7 # You may not use this file except in compliance with the License.
9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 # or http://www.opensolaris.org/os/licensing.
11 # See the License for the specific language governing permissions
12 # and limitations under the License.
14 # When distributing Covered Code, include this CDDL HEADER in each
15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 # If applicable, add the following below this CDDL HEADER, with the
17 # fields enclosed by brackets "[]" replaced with your own identifying
18 # information: Portions Copyright [yyyy] [name of copyright owner]
29 SMF_EXIT_ERR_FATAL = 95
30 SMF_EXIT_ERR_CONFIG = 96
31 SMF_EXIT_MON_DEGRADE = 97
32 SMF_EXIT_MON_OFFLINE = 98
33 SMF_EXIT_ERR_NOSMF = 99
34 SMF_EXIT_ERR_PERM = 100
35 #SMF_EXIT_ERR_OTHER = non-zero
38 # Commonly used command paths
39 PFCMD = "/usr/bin/pfexec"
40 SVCSCMD = "/usr/bin/svcs"
41 SVCADMCMD = "/usr/sbin/svcadm"
42 SVCCFGCMD = "/usr/sbin/svccfg"
43 SVCPROPCMD = "/usr/bin/svcprop"
46 class SMFInstance(Exception):
48 def __init__(self, instanceName):
49 self.instanceName = instanceName
50 self.svcstate = self.get_service_state()
51 self.svcdeps = self.get_service_dependencies()
54 def get_service_dependencies(self):
55 cmd = [SVCSCMD, "-H", "-o", "fmri", "-d", self.instanceName]
56 outdata,errdata = util.run_command(cmd)
57 result = outdata.rstrip().split("\n")
60 def get_verbose(self):
61 cmd = [SVCPROPCMD, "-c", "-p", \
62 DAEMONPROPGROUP + '/' + "verbose", \
64 outdata,errdata = util.run_command(cmd)
65 result = outdata.rstrip()
71 def find_dependency_errors(self):
73 #FIXME - do this in one pass.
74 for dep in self.svcdeps:
75 cmd = [SVCSCMD, "-H", "-o", "state", dep]
76 outdata,errdata = util.run_command(cmd)
77 result = outdata.rstrip()
78 if result != "online":
79 errors.append("%s\t%s" % (result, dep))
82 def get_service_state(self):
83 cmd = [SVCSCMD, "-H", "-o", "state", self.instanceName]
84 outdata,errdata = util.run_command(cmd)
85 result = outdata.rstrip()
88 def get_prop(self, propgroup, propname):
89 cmd = [SVCPROPCMD, "-c", "-p", \
90 propgroup + '/' + propname,\
92 outdata,errdata = util.run_command(cmd)
93 result = outdata.rstrip()
97 def set_prop(self, propgroup, propname, proptype, value):
98 cmd = [SVCCFGCMD, "-s", self.instanceName, "setprop", \
99 propgroup + '/' + propname, "=", proptype + ":", \
101 util.run_command(cmd)
102 self.refresh_service()
104 def set_string_prop(self, propgroup, propname, value):
105 cmd = [SVCCFGCMD, "-s", self.instanceName, "setprop", \
106 propgroup + '/' + propname, "=", "astring:",
108 util.run_command(cmd)
109 self.refresh_service()
111 def set_boolean_prop(self, propgroup, propname, value):
116 self.set_prop(propgroup, propname, "boolean", strval)
118 def set_integer_prop(self, propgroup, propname, value):
119 self.set_prop(propgroup, propname, "integer", str(value))
121 def refresh_service(self):
122 cmd = [SVCADMCMD, "refresh", self.instanceName]
123 util.run_command(cmd)
125 def disable_service (self):
126 if self.svcstate == "disabled":
128 cmd = [SVCADMCMD, "disable", self.instanceName]
129 util.run_command(cmd)
130 self.svcstate = self.get_service_state()
132 def enable_service (self):
133 if (self.svcstate == "online" or self.svcstate == "degraded"):
135 cmd = [SVCADMCMD, "enable", self.instanceName]
136 util.run_command(cmd)
137 self.svcstate = self.get_service_state()
139 def mark_maintenance (self):
140 cmd = [SVCADMCMD, "mark", "maintenance", self.instanceName]
141 util.run_command(cmd)
144 ret = "SMF Instance:\n" +\
145 "\tName:\t\t\t%s\n" % (self.instanceName) +\
146 "\tState:\t\t\t%s\n" % (self.svcstate)
150 if __name__ == "__main__":
151 S = SMFInstance('svc:/application/time-slider')