Checkin of 0.2.98 upstream source
[time-slider.git] / usr / share / time-slider / lib / time_slider / timeslidersmf.py
1 #!/usr/bin/python2.6
2 #
3 # CDDL HEADER START
4 #
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.
8 #
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.
13 #
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]
19 #
20 # CDDL HEADER END
21 #
22
23 import subprocess
24 import threading
25 import smf
26 import util
27
28 #SMF EXIT CODES
29 SMF_EXIT_OK          = 0
30 SMF_EXIT_ERR_FATAL   = 95
31 SMF_EXIT_ERR_CONFIG  = 96
32 SMF_EXIT_MON_DEGRADE = 97
33 SMF_EXIT_MON_OFFLINE = 98
34 SMF_EXIT_ERR_NOSMF   = 99
35 SMF_EXIT_ERR_PERM    = 100
36 #SMF_EXIT_ERR_OTHER = non-zero
37
38 cleanupTypes = ("warning", "critical", "emergency")
39
40 SMFNAME = 'svc:/application/time-slider'
41 ZFSPROPGROUP = "zfs"
42 ZPOOLPROPGROUP = "zpool"
43 DAEMONPROPGROUP = "daemon"
44
45 # Commonly used command paths
46 PFCMD = "/usr/bin/pfexec"
47 SVCSCMD = "/usr/bin/svcs"
48 SVCADMCMD = "/usr/sbin/svcadm"
49 SVCCFGCMD = "/usr/sbin/svccfg"
50 SVCPROPCMD = "/usr/bin/svcprop"
51
52
53 class TimeSliderSMF(smf.SMFInstance):
54
55     def __init__(self, instanceName = SMFNAME):
56         smf.SMFInstance.__init__(self, instanceName)
57         self._cleanupLevels = {}
58         self._cleanupLevelsLock = threading.Lock()
59
60     def get_keep_empties(self):
61         if self.get_prop(ZFSPROPGROUP, "keep-empties") == "true":
62             return True
63         else:
64             return False
65
66     def is_custom_selection(self):
67         value = self.get_prop(ZFSPROPGROUP, "custom-selection")
68         if value == "true":
69             return True
70         else:
71             return False
72
73     def get_separator(self):
74         result = self.get_prop(ZFSPROPGROUP, "sep")
75         if len(result) != 1:
76             raise ValueError("zfs/sep must be a single character length")
77         return result
78
79     def get_remedial_cleanup(self):
80         value = self.get_prop(ZPOOLPROPGROUP, "remedial-cleanup")
81         if value == "false":
82             return False
83         else:
84             return True
85
86     def get_cleanup_level(self, cleanupType):
87         if cleanupType not in cleanupTypes:
88             raise ValueError("\'%s\' is not a valid cleanup type" % \
89                            (cleanupType))
90         self._cleanupLevelsLock.acquire()
91         value = self.get_prop(ZPOOLPROPGROUP, "%s-level" % (cleanupType))
92         self._cleanupLevelsLock.release()
93         return int(value)
94
95     def set_cleanup_level(self, cleanupType, level):
96         if cleanupType not in cleanupTypes:
97             raise ValueError("\'%s\' is not a valid cleanup type" % \
98                            (cleanupType))
99         if level < 0:
100             raise ValueError("Cleanup level value can not not be negative")
101         if cleanupType == "warning" and \
102             level > self.get_cleanup_level("critical"):
103             raise ValueError("Warning cleanup level value can not exceed " + \
104                              "critical cleanup level value")
105         elif cleanupType == "critical" and \
106             level > self.get_cleanup_level("emergency"):
107             raise ValueError("Critical cleanup level value can not " + \
108                              "exceed emergency cleanup level value")
109         elif level > 100: # Emergency type value
110             raise ValueError("Cleanup level value can not exceed 100")
111
112         self._cleanupLevelsLock.acquire()
113         propname = "%s-level" % (cleanupType)
114         self.set_integer_prop(ZPOOLPROPGROUP, propname, level)
115         self._cleanupLevels[cleanupType] = level
116         self._cleanupLevelsLock.release()
117         self.refresh_service()
118
119     def set_custom_selection(self, value):
120         self.set_boolean_prop(ZFSPROPGROUP, "custom-selection", value)
121         self.refresh_service()
122
123     def get_verbose(self):
124         value = self.get_prop(DAEMONPROPGROUP, "verbose")
125         if value == "true":
126             return True
127         else:
128             return False
129
130     def __eq__(self, other):
131         if self.fs_name == other.fs_name and \
132            self.interval == other.interval and \
133            self.period == other.period:
134             return True
135         return False
136         
137     def __str__(self):
138         ret = "SMF Instance:\n" +\
139               "\tName:\t\t\t%s\n" % (self.instanceName) +\
140               "\tState:\t\t\t%s\n" % (self.svcstate) + \
141               "\tVerbose:\t\t%s\n" % str(self.get_verbose()) + \
142               "\tCustom Selction:\t%s\n" % str(self.is_custom_selection()) +\
143               "\tKeep Empties:\t\t%s\n" % str(self.get_keep_empties()) +\
144               "\tWarning Level:\t\t%d\n" % (self.get_cleanup_level("warning")) + \
145               "\tCritical Level:\t\t%d\n" % (self.get_cleanup_level("critical")) + \
146               "\tEmergency Level:\t%d\n" % (self.get_cleanup_level("emergency")) + \
147               "\tSeparator Char:\t\t\'%s\'" % (self.get_separator())
148         return ret
149
150
151 if __name__ == "__main__":
152   S = TimeSliderSMF('svc:/application/time-slider')
153   print S
154