Xׯd,dZddlZddlmZmZmZmZmZddlZddlm Z m Z m Z m Z m Z ddlZddlmZmZmZmZmZmZddlmZddlmZmZddlmZmZmZmZm Z Gd d ej!Z"Gd d e"Z#Gd deZ$GddeZ%dS)a How to handle MySQL logs ************************* Among other things this module needs to establish: 1. Which of the logs (if any) are enabled. 2. Whether the output of the enabled logs are stored in a table or in a log file. Determining the state of the logs ----------------------------------- There are two possible scenarios: the server is down: In this case all logs are assumed to be **disabled** since the server won't be logging anything unless it's running. the server is up: Starting with MySQL Server 5.1.29 the relevant server variables changed. This derives in two new scenarios: server version < 5.1.29: :general query log: The state of this log is reflected in the server variable ``log`` which will be set to ``'OFF'`` if the log is disabled or to either ``'ON'`` or to ``'1'`` or to a file path if the log is enabled. :slow query log: Its state is reflected in the server variable ``log_slow_queries`` which will be set to ``'OFF'`` if the log is disabled or to ``'ON'`` or to ``'1'`` if the log is enabled. :error log: Its state is reflected in the server variable ``log_error`` which will be set to ``''`` (the empty string) if the log is disabled or to the path to the error log file if the log is enabled. .. note:: The error logs are not going to be written to the error log file if the server was started with the ``--console`` command line option on Windows, even if ``log_error`` has a path set up. In this case error logs are written to stderr. It seems that there's no way to query the server to determine if this ``--console`` option is in place. .. note:: The ``log_slow_queries`` and ``log`` variable were removed in MySQL Server 5.6. server version >= 5.1.29: :general query log: If enabled the ``general_log`` variable will be set to ``'ON'`` or to ``'1'``. Otherwise it will be set to ``'OFF'`` or to ``'0'``. :slow query log: If enabled the ``slow_query_log`` variable will be set to ``'ON'`` or to ``'1'``. Otherwise it will be set to ``'OFF'`` or to ``'0'``. :error log: Its state is reflected in the server variable ``log_error`` as explained for server version < 5.1.29. .. note:: For MySQL Server versions >= 5.1.6, if the variable ``log_output`` contains the value ``'NONE'``, the general query log and the slow query log will not record any new log entry. The general log can also be disabled by setting to ``'1'`` the session variable ``sql_log_off``. Determining where the logs are being written --------------------------------------------- Starting from MySQL Server 5.1.6 there's the possibility of sending the logs from the general query log or from the slow query log to a server table instead of to a log file. This is controlled by the value of the variable ``log_output`` which can be a combination of the values ``'FILE'`` (log into log files), ``'TABLE'`` (log into a server table) or ``'NONE'`` (don't log). If given, ``'NONE'`` takes precedence over the other values. .. warning:: For MySQL Server versions older than 5.1.29 there's no reliable way to know where the general and slow query logs are being written (if they are logged to a file). Their default values are ``$datadir + $hostname + '.log'`` and ``$datadir + $hostname + '-slow.log'``, respectively. This can be changed in the command line or in the configuration file and the real paths will not be exposed in any server variable. Starting from MySQL Server 5.1.29 the paths to the general and slow query log files are stored in the ``general_query_log_file`` and in the``slow_query_log_file`` variables, respectively. *The server creates the file in the data directory unless an absolute path name is given to specify a different directory.* Use cases for the server logs --------------------------------- 1. The server is running. All logs are enabled and WB can precisely determine their sources. Every log is shown in its corresponding tab. 2. The ``log_output`` server variable contains both ``'FILE'`` and ``'TABLE'``. WB will select on a per log basis which source should be chosen. Will select table logs if the server is local or if the server is remote and any of these happen: * Cannot reliably determine the paths to the log file. * The user running WB has no read permissions on the log file. * The log file does not exist. * The user explicitly requested to read from log table. 3. ``log_output`` contains ``'NONE'``. Nonetheless WB will check for log sources and will show them, alerting the user about log not being updated because of that. *What to do if there are log files and log tables available in this case?* 4. The server is logging to files and WB can precisely determine their paths but the log file is missing in the file system. The situation will be reported to the user (advising him to ``FLUSH LOGS`` or restart the server and start querying) The log will not be shown as long as this situation persists. 5. The server is logging to tables and the connection to the server is lost. If the server is also logging to files, and a log file exists in the file system, the log file will be shown and the user will be warned about this change. 6. The server is logging to files and WB cannot precisely determine the path to the log file (i.e. server version < 5.1.29). WB will try to get the path from these sources in this order: 1. The server's configuration file. 2. The default path assumed by the server when no log path is specified. If no such file is found in the file system the log will not be shown. 7. The user changed the source of a log or the path to a log file while WB was showing the previous source/path. WB will switch to the new source/path and will inform the user of the change. 8. [Postponed] The user clicks on a *Add new log* button. A dialog will be shown in which the user will select the type of log to be added (error, general or slow), the log source (a database table or a log file) and the name of the log table/file. A new tab showing the user selected log will be shown and will persist during the session or until the user clicks on the tab's *Close* button. N)newBoxnewLabel newTreeView newTabView newButton)GeneralQueryLogReaderSlowQueryLogReaderGeneralLogFileReaderSlowLogFileReaderErrorLogFileReader)LogFileAccessError ServerIOErrorOperationCancelledErrorInvalidPasswordErrorlog_error_decorator to_unicode)WorkerThreadHelper) log_error log_debug)WbAdminTabBaseWbAdminValidationConfigFileWbAdminValidationConnectionWbAdminValidationBaseWbAdminValidationRemoteAccessceZdZdZfdZedZdZdZdZ dZ dZ d Z d Z dd Zd ZdZdZdZdZdZxZS)LogViewz| Main front end view class for displaying log entries. Each page in the Tab View is an instance of this class. cNtt|d||||_||_||_d|_d|_ d|_ d|_ d|_ | d|_dS)z The arguments following BackendLogReaderClass will be passed to BackendLogReaderClass to instantiate it in order to get a log reader object. FN)superr__init__ set_managedset_release_on_addownerBackendLogReaderClassargs log_reader error_boxtreebbox warning_box update_uiworkerselfr"r#r$ __class__s ..\modules\wb_admin_logs.pyrzLogView.__init__s gt%%e,,,  !!! %:"     c |jr!||jd|_td|_|jdt t |}|j|ddt}|d|j|dd| |j ||jdddS)NTFz Try again) r&remover set_spacingrstraddrset_textadd_clicked_callback try_again)r-error error_labeltry_again_buttons r/ _show_errorzLogView._show_errors > " KK ' ' '!DN ""1%%%s5zz**  ;t444$;;!!+... +UD999--dn=== -----r0cV||dSN)r*refreshr-s r/r9zLogView.try_agains#  r0cdSr?rAs r/create_filter_boxzLogView.create_filter_box r0c X|jr!||jd|_|jr!||jd|_|jr!||jd|_|jr!||jd|_|d|d|}|r@|r||| |dd |j |j |_ n\#t$rO}ddl}td|d|d|d|zYd}~dSd}~wwxYw|j jrt'd|_|jdt)|j j}|j |dd| |jddt+t,j|_|jt,j|j jD]5}|jt,j|d|dd6|j| |jddt-j}|d | d |!d |"d | t)d ddddt,j#|j j$rd|j j$j%d kr0d |_&tOj(|j)t)d}n.t)|j j$j%}nt)d}|*t,j+| |dd ddt,j,t,j#zt,j-z| t)dd dddt,j,t,j#zt)|j j$rdnd|_.|j.*t,j+| |j.dd ddt,j,t,j#zt,j-z| t)ddddd t,j,t,j#zt)d|_/|j/*t,j+| |j/dd dd t,j,t,j#z| |ddt'd|_|jd|0|jddt-j1|_2|j23d|j4dd|j23d|j5dd|j6|j2|j t)dddto|_8|j89d|j |j8dd|j8:|j;to|_<|j<9d|j |j<dd|j<:|j=to|_>|j>9d|j |j>dd|j>:|j?to|_@|j@9d|j |j@dd|j@:|jAto|_B|jB9d|j |jBdd|jB:|jC|j j$r8|j j$j%d kr%d|_DtOjEd|j&dSdSdS)Nr2FTrzException creating log reader:  zError creating log reader: %s zLog File Location:stderra zWindows Event viewerTABLEzLog File Size:z retrieving...-zShowing:zretrieving data...zCopy Rowz Copy Recordz Copy DetailsOldestz< Previous Pagez Next Page >z Most RecentRefresh)Fr&r3r'r(r) set_paddingr4rD get_parentr6r#r$r% Exception tracebackr format_excr=partial_supportrrrmforms TreeFlatListset_selection_modeTreeSelectMultiple column_specs add_columnStringColumnType end_columnsnewTableset_row_spacingset_column_spacingset_column_count set_row_count HFillFlaglog_filepathquerygrtsetEventlogCallback printResults set_style BoldStyle VFillFlag HExpandFlag size_label range_labeladd_endnewContextMenu_menuadd_item_with_title copy_record copy_detailsset_context_menur bof_buttonr7r8go_bof back_buttongo_back next_buttongo_next eof_buttongo_eofrefresh_buttonr@actual_positiongetEventLogEntry)r- filter_boxr:rU warning_labelcolspectablelabels r/r*zLogView.update_uis > " KK ' ' '!DN 9  KK " " "DI 9  KK " " "DI   $ KK( ) ) )#D   ++--  .$$&& ( J''' HHZ - - - 8d8$)DDOO        I555)J^J^J`J`J`J`a b b b   >F G G G FFFFF   ? * 4%d||D    ( ( + + +$T_%DEEM   t < < < HHT%ud 3 3 3 344  $$V%>???3 ( (G I !8!(!(!& ( ( ( (  D$'''!! a      ### q!!! A (/00!Q1f>NOOO ? # &',888 '(9::: !788 !9!>??W%%E ())) %Aq!V%5f6F%FvGY%YZZZ (+,,aAq&:J6K[:[\\\"do6N#W??TWXX !!&"2333 $/1aAv/?@P/PQWQc/cddd (:&&1aF4DVEU4UVVV#$899 ""6#3444 $"Aq!Q0@AQ0QRRR t$$$4LL  a    TYt,,,*,,  &&z43C]Tabbb &&~t7H.Zhiii ""4:... hrllD$///#++   *** doud333 ,,T[999$;; !!"3444 d&t444 --dl;;;$;; !!-000 d&t444 --dl;;;#++   /// doud333 ,,T[999'kk $$Y/// d)5$777 00>>> ? # 0(@(E(Q(Q#$D  DJ / / / / / 0 0(Q(Qs"D77 FAF  Fc$|jdz|_|j}|jt |jt |jjD]!\}}| |||"dS)NrH) rr'add_noderpr7r5 enumerater% column_keys set_string)r-textr$rowidxkeys r/rkzLogView.printResultsms#3a7i  ""   T%9!:!:;;;!$/"=>> + +HC NN3S * * * * + +r0cJ||jdSr?)r%current)r-outs r/read_data_workerzLogView.read_data_workerts& DO # # % %&&&&&r0ct|tr/tjdd|zdddd|_dS||d|_dS)NzError Reading Log Filez%sOKrO) isinstancerTrX Utilities show_errorr+update)r-datas r/handle_worker_datazLogView.handle_worker_datawsk dI & &    ' '(@(,t T2r C C CDK F D r0c|jr|j|js|jd|jd|jd|jd|jdt|j |j |_|j ddSdSdS)NFrH) r%r@r+ry set_enabledr{rr}rrrrstartrAs r/r@zLogView.refreshs ? % O # # % % %; %++E222 ,,U333++E222 ,,U333#//66611FH_``  !!!$$$$$ % % % %r0Nc |jr|jjr|jjjdkrtj|j|j|jd|j d|j d|j d|j d|jzdS |j|D]X}|j}t#|D]-\}}|||.Y|j |j|j |j|j|j|j |j|j |j|j |j|jddS#t4t6t8t:t<t>t@f$r }|!|Yd}~dSd}~wwxYwdS)NrKFzRecords read: %dT)"r%rfrgrirrrhryrr{rr}rqr7r'clearrrrstrip range_textrp size_text has_previoushas_nextrr RuntimeErrorr rrIOError ValueErrorr=)r-recordsrecrrcolr:s r/rzLogView.updates ? (' DO,D,IX,U,U$T%94:FFF++E222 ,,U333++E222 ,,U333 ))*z'LogView.copy_record..sFFFA1Q4FFFr0rG) r'rrr%r\rangelenrrrXrr)r-rrrrecordis r/rvzLogView.copy_recordsI++--  FF)EFFFGGH + +DF3t;<<== 2 2 dooa001111 KK &)) * * * *  A   / / $ @ @ @ @ @ A Ar0c  ||jdS#ttt t tttf$r }| |Yd}~dSd}~wwxYwr?) rr%firstrrr rrrrr=r-r:s r/rzzLogView.go_bofs $ KK--// 0 0 0 0 0|-?AXZnpwzDE $ $ $   U # # # # # # # # # $,0/A?A::A?c  ||jdS#ttt t tttf$r }| |Yd}~dSd}~wwxYwr?) rr%lastrrr rrrrr=rs r/rzLogView.go_eofs $ KK,,.. / / / / /|-?AXZnpwzDE $ $ $   U # # # # # # # # # $rc > |jr|jnd}||dS#tt t ttttf$r }| |Yd}~dSd}~wwxYwr?) r%rpreviousrrrr rrrrr=r-rr:s r/r|zLogView.go_backs $48O4P4P4R4R\do..000X\G KK |-?AXZnpwzDE $ $ $   U # # # # # # # # # $sA A /B<BBc 4 |jrt|jnd}||dS#tt t ttttf$r }| |Yd}~dSd}~wwxYwr?) r%rnextrrrr rrrrr=rs r/r~zLogView.go_nexts $/3/G/G/I/ISd4?+++tG KK |-?AXZnpwzDE $ $ $   U # # # # # # # # # $sAA/B7BBr?)__name__ __module__ __qualname____doc__rrr=r9rDr*rkrrr@rrwrvrzrr|r~ __classcell__r.s@r/rrs? 0 . . .   {0{0{0z+++''' % % %((((4AAA A A A$$$ $$$ $$$$$$$$$$r0rc<eZdZfdZdZdZdZdfd ZxZS)LogViewGenericci|_tjd|_d|_t t |j||g|RdS)NTrO) filter_listrXrr filter_textrrrr,s r/rzLogViewGeneric.__init__sR ---,nd##,U4IQDQQQQQQr0ctjd}||||j||j|<|j|dddS)NrHFT)rXnewRadioButtonr7r8filter_handlerrrr6r-rfilters r/add_filter_optionz LogViewGeneric.add_filter_optionsh&q))##D$7888!' FE400000r0c|d|dtjjjjdks|d|jS)NAllInnoDB CommunityFirewall)rrirootwbinfoeditionrrAs r/rDz LogViewGeneric.create_filter_boxs[ u%%% x(((x{';66  " ": . . .r0c|jD] \}}|r||_!|dSr?)ritems get_activerr@rs r/rzLogViewGeneric.filter_handlersR ,2244 ( (LD&  "" (#'  r0Nc\d}|r~g}|D]y}t|d}|jdksB||jdkr||zt t ||dS)NrMrr)rrlowerfindrrrr)r-rfiltered_recordsrrr.s r/rzLogViewGeneric.updates  4! ! 4 4!&),,#u,, 0A0A$BRBXBXBZBZ0[0[_`0`0`$++F333 nd##**+;<<<<.open_remote_fileNs0000 )"&,"2"="="?"?C//0CEVEfEfgkgzE{E{|||"00A55 # # #'4()))%&GNNN#(55)sBBB98B9 general_logslow_log stored_general_log_source_choicestored_slow_log_source_choice)ris_sql_connectedtarget_version#is_supported_mysql_version_at_least exec_querynextRow stringByNamerrrr.r admin_enabledgetattrgeneral_log_file_pathis_localr$r)slow_log_file_pathr*)r-destresultr&s` r/get_log_destinationzWbAdminLogs.get_log_destination8s < ( ( * *C V<.RRSTUVWXYYB V !\445PQQF!>>++$# $ KKK:@:M:Mg:V:VX\:\:\bh"-T^5L5L5LdN`NkNkNklnnnn !\445WXXF!>>++$# $ KKK060C0CG0L0L"-T/:::w$J\Jg?g?g ) ) ) ) ) -;$.5]++2Z(!T^=T=T=TVZVZVZ"[]]]# #4)KTRR T-C:>RVRdRm!nT%7%M N N NtDtDEIEWEmtntn6<] 3 3>6=] 3 3 329D/@D]@S==.2.S]+"4)H$OO N-@7;OSOaOj!hT%7%J K K KqAqABFBTBgqhqh39Z 0 0;3:Z 0 0 0/6D,=A*=M::+/+MZ(T^5L5L5LdddSUUU s0/A11A7 /C>>D?GG'%?I%%I.c|j||jd|jddS)NTF)warningr7showr)r-msgs r/show_message_panelzWbAdminLogs.show_message_panelsJ c""" $ %     r0cXd|vrt|ddgz}d|vrt|ddgz}d|vr-|jr&|j|jd|_d|vr-|jr&|j|jd|_d|vr-|jr&|j|jd|_d|vr-|jr&|j|jd|_d|vr/|jr*|j|jd|_dSdSdS)NrL general_tabslow_tabrgeneral_file_tab slow_file_taberror_file_tab)listrr remove_pagerrrrr-sources r/ _remove_tabszWbAdminLogs._remove_tabss[ f  &\\]J$??F V  &\\%7$IIF F " "t'; " L $ $T%9 : : :#'D   D$5  L $ $T%6 7 7 7#D   ' 'D,E ' L $ $T%> ? ? ?(,D % f $ $)? $ L $ $T%; < < <%)D " v % %$*A % L $ $T%< = = =&*D # # # & % % %r0cd|vrt|ddgz}d|vrt|ddgz}d|vr|jjr|js t |t |j|jj|_|j|jdn9#t$r,| d |jjzd|_YnwxYwd|vr|jj r|jj r|j s t|t|j|jj |_ |j|j d n9#t$r,| d |jj zd|_ YnwxYwd|vr|jjr|jjr|js t|t&|j|jj|_|j|jd n9#t$r,| d |jjzd|_YnwxYwd|vrS|jj rG|js@t|t*|j|_|j|jd d|vrU|jjrK|jsFt|t.|j|_|j|jddSdSdSdS)NrLr?r@rrArBrCzError Log FilezYThere was a problem reading %s. Please verify that you have read permissions on that filezGeneral Log FilezYThere was a problem reading %s. Please verify that you have read permissions on that filez Slow Log FilezGeneral Log TablezSlow Query Log Table)rDrerror_log_file_pathrrr rradd_pagerr=general_log_enabledr3rrr slow_log_enabledr5rr rrrr rFs r/ _add_tabszWbAdminLogs._add_tabss f  &\\]J$??F V  &\\%7$IIF v % %$*<*P %Y]Yp % /*8?QSWS_aeasbH+I+I' %%d&=?OPPPP / / /'')DGKGYGm)mnnn*.''' /  ' 'D,>,R 'W[WiW 'IMIb ' 1,3D:NPTP\^b^p_G-H-H) %%d&?ASTTTT 1 1 1'')EHLHZHp)pqqq,0))) 1 f $ $);)L $QUQcQv $@D@V $ .)07H$,X\XjX})~)~& %%d&>3E43E4A G''3HHc|}|d|jjs d|j_d|jjvr|ddnL|jjdkr,|d|dn|jjdkr+|d|dn|jjdks|jjdkrt gd}g}|jsdd g}nF||d dkrdnd ||d dkrd nd |jt|t |z |jt||j d | dS#t$rN}tjdd|zddd}|tjkr d|_Yd}~dSYd}~dSd}~wwxYw)NrCrrrLrr)r?r@rArBrArBr'r?r(r@Tz Log Refreshz8An error occurred while displaying MySQL server logs: %sIgnoreCancelrO)r8rNrrrHsetrr+rtuplerr;r@rTrXr show_warning ResultCancelr )r-r6tabs to_be_addeders r/r*zWbAdminLogs.update_uis'')) '(((!, 9,8D  ) T'2 2 2   gv . . . .   *f 4 4   g & & & NN6 " " " "   *g 5 5   f % % % NN7 # # # #   *l : :d>P>[_k>k>kWWWXXDK<0022 b1?C ""m9LPV9V9V#5#5\ijjj""d:6F&6P6P??V`aaa D dS-=-=&= > > @ @ DNU;// 1 1 $ 0 LLNNNNN 0 0 0 --m=wz{={~FHPRTUUAF'''+/((((((((''''' 0sG%% H=/=H88H=cp|jd}|dvrt|jj|jj|jj|jjgsUtj |j|j}| |jj dt| |jj}|jjsd|vr|d|j_|jjs;d|vr|dn d|vr|dnd }|r|d |j_|jjs;d |vr|d n d |vr|d nd }|r|d |j_|jjs/d |vr-|d d |j_dSdSdSdSdS)Nr)verbose)stoppedunknownF) warn_missingz log-outputgeneral_log_filelogrO"slow_query_log_filezlog-slow-queriesz log-error)ris_server_runningallrr3r5rJrwb_admin_config_file_beWbAdminConfigFileBEopen_configuration_fileconfig_file_pathdict get_optionsconfig_file_section log_ouputr)r-statuscfg_beoptionsrgs r/rzWbAdminLogs.detect_pathss///:: + + +C$:$7$8$/25454 + -@ASUYUabbF  * *4+=+N]b * c c c6--d.@.TUUVVG%0 E\W5L5L/6|/D",%; O6HG6S6Sw122).')9)9wu~~rO?Czz#D&<%8 L9NRY9Y9Yw4556HG6S6Sw122Y[L<@JJsOOD&9%9 YkW>T>T9@9M9S9STW9X9X"6665 , + + +2 Y Y>T>Tr0c|jrdS|jr@|jr|j|jr|j|jr|j|jr|j|jr|jdSdSr?) r rr+rr@rrrrrAs r/r@zWbAdminLogs.refresh s  #  F < ( ( * * ,# /$,,...  ,!))+++  $ 0  % - - / / /  ! -  " * * , , ,  " .  # + + - - - - - . .r0cdSr?rCrAs r/ do_refreshzWbAdminLogs.do_refreshrEr0N)rrr ui_created classmethodrrrrrr8r=rHrNr*rr@rqrCr0r/rr sJYY[Y##[#''' [ [ [   FFFP!!! +++.&M&M&MP%0%0%0NYYY@ . . .     r0r)&rrirXrrrrr wb_log_readerrr r r r rd wb_commonr rrrrrworkbench.utilsr workbench.logrrwb_admin_utilsrrrrrBoxrrrrrCr0r/rzs_,VVp GGGGGGGGGGGGGG AAAAAAAAAAAAAAHHHHHHHHHHHHHHHH..............ZZZZZZZZZZZZZZW$W$W$W$W$fjW$W$W$t'='='='='=W'='='=T o o o o o%: o o oN N N N N .N N N N N r0