Debian Codegraph @

troubleshoot/ErrorLogCheckpoint.py

raw
1 #!/usr/bin/python3
2
3 ## Printing troubleshooter
4
5 ## Copyright (C) 2008, 2009, 2014 Red Hat, Inc.
6 ## Author: Tim Waugh <[email protected]>
7
8 ## This program is free software; you can redistribute it and/or modify
9 ## it under the terms of the GNU General Public License as published by
10 ## the Free Software Foundation; either version 2 of the License, or
11 ## (at your option) any later version.
12
13 ## This program is distributed in the hope that it will be useful,
14 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ## GNU General Public License for more details.
17
18 ## You should have received a copy of the GNU General Public License
19 ## along with this program; if not, write to the Free Software
20 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
22 from gi.repository import Gtk
23
24 import cups
25 import os
26 from tempfile import NamedTemporaryFile
27 import datetime
28 import time
29 from timedops import TimedOperation, OperationCanceled
30 from .base import *
31
32 try:
33 from systemd import journal
34 except:
35 journal = False
36
37 class ErrorLogCheckpoint(Question):
38 def __init__ (self, troubleshooter):
39 Question.__init__ (self, troubleshooter, "Error log checkpoint")
40 page = self.initial_vbox (_("Debugging"),
41 _("This step will enable debugging output "
42 "from the CUPS scheduler. This may "
43 "cause the scheduler to restart. Click "
44 "the button below to enable debugging."))
45 button = Gtk.Button.new_with_label (_("Enable Debugging"))
46 buttonbox = Gtk.HButtonBox ()
47 buttonbox.set_border_width (0)
48 buttonbox.set_layout (Gtk.ButtonBoxStyle.START)
49 buttonbox.pack_start (button, False, False, 0)
50 self.button = button
51 page.pack_start (buttonbox, False, False, 0)
52 self.label = Gtk.Label ()
53 self.label.set_alignment (0, 0)
54 self.label.set_line_wrap (True)
55 page.pack_start (self.label, False, False, 0)
56 troubleshooter.new_page (page, self)
57 self.persistent_answers = {}
58
59 def __del__ (self):
60 if not self.persistent_answers.get ('error_log_debug_logging_set',
61 False):
62 return
63
64 f = self.troubleshooter.answers['_authenticated_connection_factory']
65 c = f.get_connection ()
66 c._set_lock (False)
67 settings = c.adminGetServerSettings ()
68 if len (list(settings.keys ())) == 0:
69 return
70
71 settings[cups.CUPS_SERVER_DEBUG_LOGGING] = '0'
72 answers = self.troubleshooter.answers
73 orig_settings = self.persistent_answers['cups_server_settings']
74 settings['MaxLogSize'] = orig_settings.get ('MaxLogSize', '2000000')
75 c.adminSetServerSettings (settings)
76
77 def display (self):
78 self.answers = {}
79 answers = self.troubleshooter.answers
80 if not answers['cups_queue_listed']:
81 return False
82
83 self.authconn = answers['_authenticated_connection']
84 parent = self.troubleshooter.get_window ()
85
86 def getServerSettings ():
87 # Fail if auth required.
88 cups.setPasswordCB (lambda x: '')
89 cups.setServer ('')
90 c = cups.Connection ()
91 return c.adminGetServerSettings ()
92
93 try:
94 self.op = TimedOperation (getServerSettings, parent=parent)
95 settings = self.op.run ()
96 except RuntimeError:
97 return False
98 except cups.IPPError:
99 settings = {}
100
101 self.forward_allowed = False
102 self.label.set_text ('')
103 if len (list(settings.keys ())) == 0:
104 # Requires root
105 return True
106 else:
107 self.persistent_answers['cups_server_settings'] = settings
108
109 try:
110 if int (settings[cups.CUPS_SERVER_DEBUG_LOGGING]) != 0:
111 # Already enabled
112 return False
113 except KeyError:
114 pass
115 except ValueError:
116 pass
117
118 return True
119
120 def connect_signals (self, handler):
121 self.button_sigid = self.button.connect ('clicked', self.enable_clicked,
122 handler)
123
124 def disconnect_signals (self):
125 self.button.disconnect (self.button_sigid)
126
127 def collect_answer (self):
128 answers = self.troubleshooter.answers
129 if not answers['cups_queue_listed']:
130 return {}
131
132 parent = self.troubleshooter.get_window ()
133 self.answers.update (self.persistent_answers)
134 if 'error_log_checkpoint' in self.answers:
135 return self.answers
136
137 tmpf = NamedTemporaryFile()
138 try:
139 self.op = TimedOperation (self.authconn.getFile,
140 args=["/admin/log/error_log"],
141 kwargs={'file': tmpf},
142 parent=parent)
143 self.op.run ()
144 except (RuntimeError, cups.IPPError) as e:
145 self.answers['error_log_checkpoint_exc'] = e
146 except cups.HTTPError as e:
147 self.answers['error_log_checkpoint_exc'] = e
148
149 # Abandon the CUPS connection and make another.
150 answers = self.troubleshooter.answers
151 factory = answers['_authenticated_connection_factory']
152 self.authconn = factory.get_connection ()
153 self.answers['_authenticated_connection'] = self.authconn
154
155 try:
156 statbuf = os.stat (tmpf.name)
157 except OSError:
158 statbuf = [0, 0, 0, 0, 0, 0, 0]
159
160 self.answers['error_log_checkpoint'] = statbuf[6]
161 self.persistent_answers['error_log_checkpoint'] = statbuf[6]
162
163 if journal:
164 j = journal.Reader ()
165 j.seek_tail ()
166 cursor = j.get_previous ()['__CURSOR']
167 self.answers['error_log_cursor'] = cursor
168 self.persistent_answers['error_log_cursor'] = cursor
169 now = datetime.datetime.fromtimestamp (time.time ())
170 timestamp = now.strftime ("%F %T")
171 self.answers['error_log_timestamp'] = timestamp
172 self.persistent_answers['error_log_timestamp'] = timestamp
173
174 return self.answers
175
176 def can_click_forward (self):
177 return self.forward_allowed
178
179 def enable_clicked (self, button, handler):
180 parent = self.troubleshooter.get_window ()
181 self.troubleshooter.busy ()
182 try:
183 self.op = TimedOperation (self.authconn.adminGetServerSettings,
184 parent=parent)
185 settings = self.op.run ()
186 except (cups.IPPError, OperationCanceled):
187 self.troubleshooter.ready ()
188 self.forward_allowed = True
189 handler (button)
190 return
191
192 self.persistent_answers['cups_server_settings'] = settings.copy ()
193 MAXLOGSIZE='MaxLogSize'
194 try:
195 prev_debug = int (settings[cups.CUPS_SERVER_DEBUG_LOGGING])
196 except KeyError:
197 prev_debug = 0
198 try:
199 prev_logsize = int (settings[MAXLOGSIZE])
200 except (KeyError, ValueError):
201 prev_logsize = -1
202
203 if prev_debug == 0 or prev_logsize != '0':
204 settings[cups.CUPS_SERVER_DEBUG_LOGGING] = '1'
205 settings[MAXLOGSIZE] = '0'
206 success = False
207
208 def set_settings (connection, settings):
209 connection.adminSetServerSettings (settings)
210
211 # Now reconnect.
212 attempt = 1
213 while attempt <= 5:
214 try:
215 time.sleep (1)
216 connection._connect ()
217 break
218 except RuntimeError:
219 # Connection failed
220 attempt += 1
221
222 try:
223 debugprint ("Settings to set: " + repr (settings))
224 self.op = TimedOperation (set_settings,
225 args=(self.authconn, settings,),
226 parent=parent)
227 self.op.run ()
228 success = True
229 except cups.IPPError:
230 pass
231 except RuntimeError:
232 pass
233
234 if success:
235 self.persistent_answers['error_log_debug_logging_set'] = True
236 self.label.set_text (_("Debug logging enabled."))
237 else:
238 self.label.set_text (_("Debug logging was already enabled."))
239
240 self.forward_allowed = True
241 self.troubleshooter.ready ()
242 handler (button)
243
244 def cancel_operation (self):
245 self.op.cancel ()
246
247 # Abandon the CUPS connection and make another.
248 answers = self.troubleshooter.answers
249 factory = answers['_authenticated_connection_factory']
250 self.authconn = factory.get_connection ()
251 self.answers['_authenticated_connection'] = self.authconn