Skip to main content
Glama

PTP MCP Server

by aneeshkp
ptp_log_parser.cpython-311.pyc26.4 kB
� x�|h�G����dZddlZddlZddlZddlZddlmZmZmZm Z m Z ddl m Z m Z ddl mZeje��ZeGd�d����ZGd�d ��ZdS) zR PTP Log Parser - Parses linuxptp daemon logs and extracts structured information �N)�Dict�List�Optional�Any�Tuple)�datetime� timedelta)� dataclassc�V�eZdZUdZeed<eed<eed<eed<eeefed<dS)�LogEntryzStructured log entry� timestamp� component�level�message� parsed_dataN) �__name__� __module__� __qualname__�__doc__r�__annotations__�strrr���:/home/aputtur/Ai Projects/ptp-mcp-server/ptp_log_parser.pyr r sS��������������N�N�N� �J�J�J� �L�L�L��c�3�h������rr c ��eZdZdZd�Zd#dedededeefd �Z d edefd �Z d ed ede ee ffd�Z d ede ee ffd�Zd ede ee ffd�Zd ede ee ffd�Zd ede ee ffd�Zd ed ede ee ffd�Zd ede ee ffd�Zd ede ee ffd�Zd ede ee ffd�Zd ede ee ffd�Zd ede ee ffd�Zd ede ee ffd�Zd$deededededeef d�Zdedefd�Zdeede ee ffd �Zdeede ee ffd!�Zdeede ee ffd"�ZdS)%� PTPLogParserzParser for linuxptp daemon logsc �P�d|_d|_d|_dddddd d d d �|_dS) Nz openshift-ptpzlinuxptp-daemonzlinuxptp-daemon-containerz(\d{2}:\d{2}:\d{2}\.\d{6})z*ptp4l\[(\d+\.\d+)\]:\s*\[([^\]]+)\]\s*(.+)z,phc2sys\[(\d+\.\d+)\]:\s*\[([^\]]+)\]\s*(.+)z+ts2phc\[(\d+\.\d+)\]:\s*\[([^\]]+)\]\s*(.+)z!dpll\[(\d+)\]:\[([^\]]+)\]\s*(.+)z!gnss\[(\d+)\]:\[([^\]]+)\]\s*(.+)zGM\[(\d+)\]:\[([^\]]+)\]\s*(.+)zFI(\d{4})\s+(\d{2}:\d{2}:\d{2}\.\d{6})\s+(\d+)\s+([^:]+):(\d+)\]\s*(.+))r �ptp4l�phc2sys�ts2phc�dpll�gnss�gm�go_log)� namespace� daemon_name�container_name�patterns)�selfs r�__init__zPTPLogParser.__init__sE��(���,���9���7�B�F�D�8�8�4�_�  �  �� � � rN��r%�lines�since�returnc ��4�K�|��j} ddd�j��d�jd|dt|��g }|r|�d|g��t j|d d d � ��}|jd krtd |j �����|j � ��� d��}�fd�|D��S#t j $rtd���t$r0}t�dt|�������d}~wwxYw)z#Get PTP logs from OpenShift clusterN�oc�logszds/z-cz-nz--tailz--sinceT�<)�capture_output�text�timeoutrzFailed to get PTP logs: � c�b��g|]+}|������|����,Sr)�strip�_parse_log_line)�.0�liner)s �r� <listcomp>z-PTPLogParser.get_ptp_logs.<locals>.<listcomp>Js4���U�U�U�4�� � � � �U�D�(�(��.�.�U�U�UrzTimeout getting PTP logszError getting PTP logs: )r%r&r'r�extend� subprocess�run� returncode� Exception�stderr�stdoutr8�split�TimeoutExpired�logger�error)r)r%r,r-�cmd�result� log_lines�es` r� get_ptp_logszPTPLogParser.get_ptp_logs-s]����� � ���I� ��f�6�D�$4�6�6��d�)��i��#�e�*�*� �C�� /�� � �I�u�-�.�.�.��^��#��� ���F�� �A�%�%�� J�6�=� J� J�K�K�K�� �+�+�-�-�3�3�D�9�9�I�U�U�U�U�9�U�U�U� U���(� 8� 8� 8��6�7�7� 7�� � � � �L�L�<�C��F�F�<�<� =� =� =� ����� ���s�B1C�'D�'+D�Dr;c��tj|jd|��}d}|r� tj|�d��d��}|�tj��jtj��j tj��j ���}n#t$rYnwxYwi}d}d}|}tj |jd|��}|r�d}|�d ��} tj| d��}|�tj��jtj��j tj��j ���}n#t$rYnwxYw|�d ��}|�d ��}|� ||��}nj|j���D]P\} } | d vr� tj | |��} | r/| }| �d ��}|�||��}n�Qt!|ptj��||||���S)z0Parse individual log line into structured formatr N�z %H:%M:%S.%f)�year�month�day�unknown�infor$���)r r$�)r rrrr)�re�searchr(r�strptime�group�replace�nowrOrPrQ� ValueError�match�_parse_go_log_message�items�_parse_component_messager ) r)r;�timestamp_matchr rrrr�go_match� timestamp_str� comp_name�patternr_s rr9zPTPLogParser._parse_log_lineRsT���)�D�M�+�$>��E�E��� � � � �$�-�o�.C�.C�A�.F�.F� �V�V� �%�-�-�8�<�>�>�3F�h�l�n�n�Nb�hp�ht�hv�hv�hz�-�{�{� � ��� � � ��� ����� �� ������8�D�M�(�3�T�:�:�� � ��E�$�N�N�1�-�-�M� �$�-�m�]�K�K� �%�-�-�8�<�>�>�3F�h�l�n�n�Nb�hp�ht�hv�hv�hz�-�{�{� � ��� � � ��� ���� ���q�)�)�I��n�n�Q�'�'�G��4�4�W�i�H�H�K�K�'+�m�&9�&9�&;�&;� � �"� �7�� 7�7�7�����$�/�/���� )�I�#�k�k�!�n�n�G�"&�"?�"?���"S�"S�K��E� � ��1�8�<�>�>����#�  � � � s%�BB)�) B6�5B6�;A/E+�+ E8�7E8rrc��i}d|���vr)|�|�|����n�d|���vr)|�|�|����n}d|���vr)|�|�|����n>d|���vr(|�|�|����|S)zParse Go-style log messagesr!r"�stats�event)�lower�update�_parse_dpll_message�_parse_gnss_message�_parse_stats_message�_parse_event_message�r)rr�parseds rr`z"PTPLogParser._parse_go_log_message�s����� �Y�_�_�&�&� &� &� �M�M�$�2�2�7�;�;� <� <� <� <� �y���(�(� (� (� �M�M�$�2�2�7�;�;� <� <� <� <� � ���)�)� )� )� �M�M�$�3�3�G�<�<� =� =� =� =� � ���)�)� )� )� �M�M�$�3�3�G�<�<� =� =� =�� rc��i}tjd|��}|r%t|�d����|d<tjd|��}|r|�d��|d<tjd|��}|r|�d��|d<tjd|��}|r|�d��|d <tjd |��}|r�t|�d����|d <t|�d ����|d <|�d��dk|d<|�d��dk|d<|�d��dk|d<|S)zParse DPLL-related messageszoffset to (-?\d+) nsrN� offset_nszclock id (\d+)�clock_idz iface (\S+)� interfacezstate is ([^(]+)� dpll_statez`decision: Status (\d+), Offset (-?\d+), In spec (\w+), Source GNSS lost (\w+), On holdover (\w+)�statusrT�offsetrW�true�in_specrU� source_lost�� on_holdover�rXrY�intr[)r)rrr� offset_match� clock_match� iface_match� state_match�decision_matchs rrmz PTPLogParser._parse_dpll_message�s������y�!8�'�B�B� � � =�"%�l�&8�&8��&;�&;�"<�"<�F�;� ��i� 1�7�;�;� � � 6�!,�!2�!2�1�!5�!5�F�:� ��i���8�8� � � 7�"-�"3�"3�A�"6�"6�F�;� ��i� 3�W�=�=� � � 8�#.�#4�#4�Q�#7�#7�F�<� ���$G�IP�Q�Q�� � F�"�>�#7�#7��#:�#:�;�;�F�8� �"�>�#7�#7��#:�#:�;�;�F�8� � .� 4� 4�Q� 7� 7�6� A�F�9� �$2�$8�$8��$;�$;�v�$E�F�=� !�$2�$8�$8��$;�$;�v�$E�F�=� !�� rc���i}tjd|��}|r%t|�d����|d<tjd|��}|r%t|�d����|d<|S)zParse GNSS-related messageszgnss_status (\d+)rN� gnss_statusz offset (\d+)ryr)r)rrr� status_matchr�s rrnz PTPLogParser._parse_gnss_message�s������y�!5�w�?�?� � � ?�$'� �(:�(:�1�(=�(=�$>�$>�F�=� !��y��'�:�:� � � :�"�<�#5�#5�a�#8�#8�9�9�F�8� �� rc��i}tjd|��}|r0|�d��|d<|�d��|d<|S)zParse stats-related messageszstate updated for (\w+) =(\w+)rNrrT�state�rXrYr[�r)rrrr�s rroz!PTPLogParser._parse_stats_message�sW�����i� A�7�K�K� � � 3�"-�"3�"3�A�"6�"6�F�;� �)�/�/��2�2�F�7�O�� rc���i}tjd|��}|r`|�d��|d<|�d��|d<|�d��|d<|�d��|d <|S) zParse event-related messageszEdpll State (\w+), gnss State (\w+), tsphc state (\w+), gm state (\w+)rNrwrT� gnss_staterW� ts2phc_staterU�gm_stater�r�s rrpz!PTPLogParser._parse_event_message�s������i� h�jq�r�r� � � 6�#.�#4�#4�Q�#7�#7�F�<� �#.�#4�#4�Q�#7�#7�F�<� �%0�%6�%6�q�%9�%9�F�>� "�!,�!2�!2�1�!5�!5�F�:� �� rc�<�i}|dkr)|�|�|����n�|dkr)|�|�|����n�|dkr)|�|�|����n�|dkr)|�|�|����n]|dkr)|�|�|����n.|dkr(|�|�|����|S)z5Parse PTP component messages (ptp4l, phc2sys, ts2phc)rr rr!r"r#)rl�_parse_phc2sys_message�_parse_ts2phc_message�_parse_ptp4l_message�_parse_dpll_component_message�_parse_gnss_component_message�_parse_gm_messagerqs rrbz%PTPLogParser._parse_component_message�s���� � � !� !� �M�M�$�5�5�g�>�>� ?� ?� ?� ?� �(� "� "� �M�M�$�4�4�W�=�=� >� >� >� >� �'� !� !� �M�M�$�3�3�G�<�<� =� =� =� =� �&� � � �M�M�$�<�<�W�E�E� F� F� F� F� �&� � � �M�M�$�<�<�W�E�E� F� F� F� F� �$� � � �M�M�$�0�0��9�9� :� :� :�� rc�F�i}tjd|��}|r�t|�d����|d<|�d��|d<t|�d����|d<t|�d����|d <|S) zParse phc2sys messageszLCLOCK_REALTIME phc offset\s+(-?\d+)\s+(\w+)\s+freq\s+(-?\d+)\s+delay\s+(\d+)rNryrTr�rW� frequencyrU�delayr�r)rrrr_s rr�z#PTPLogParser._parse_phc2sys_message�s������ �i�kr�s�s�� � 2�"�5�;�;�q�>�>�2�2�F�8� �#�k�k�!�n�n�F�7�O�"%�e�k�k�!�n�n�"5�"5�F�;� �!�%�+�+�a�.�.�1�1�F�7�O�� rc���i}tjd|��}|rbt|�d����|d<|�d��|d<t|�d����|d<tjd|��}|r%t|�d����|d <tjd |��}|r�|�d��|d <|�d��|d <|�d��|d <|�d��|d<|�d��|d<|�d��|d<|S)zParse ts2phc messagesz+offset\s+(-?\d+)\s+(\w+)\s+freq\s+([+-]\d+)rNryrTr�rWr�znmea delay: (\d+) ns� nmea_delay_nsz~nmea sentence: ([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+)� nmea_type� nmea_time�nmea_latrU� nmea_lat_dirr}�nmea_lonrV� nmea_lon_dirr)r)rrrr_� nmea_match�nmea_sentence_matchs rr�z"PTPLogParser._parse_ts2phc_messagesk����� �H�'�R�R�� � 6�"�5�;�;�q�>�>�2�2�F�8� �#�k�k�!�n�n�F�7�O�"%�e�k�k�!�n�n�"5�"5�F�;� ��Y�6��@�@� � � ?�&)�*�*:�*:�1�*=�*=�&>�&>�F�?� #�!�i�)j�ls�t�t�� � B�"5�";�";�A�">�">�F�;� �"5�";�";�A�">�">�F�;� �!4�!:�!:�1�!=�!=�F�:� �%8�%>�%>�q�%A�%A�F�>� "�!4�!:�!:�1�!=�!=�F�:� �%8�%>�%>�q�%A�%A�F�>� "�� rc�4�i}tjd|��}|r|�d��|d<tjd|��}|rOt|�d����|d<|�d�����|d<|S)zParse ptp4l messageszselected (\w+) clockrN�selected_clockzport (\d+): ([\w\s]+)�portrT� port_state)rXrYr[r�r8)r)rrr� bmca_match� port_matchs rr�z!PTPLogParser._parse_ptp4l_message!s������Y�6��@�@� � � ;�'1�'7�'7��':�':�F�#� $��Y�7��A�A� � � ?� ��!1�!1�!�!4�!4�5�5�F�6�N�#-�#3�#3�A�#6�#6�#<�#<�#>�#>�F�<� �� rc���i}tjd|��}|r�|�d��|d<t|�d����|d<t|�d����|d<t|�d����|d <t|�d ����|d <|�d ��|d <|S)zParse DPLL component messageszU(\w+) frequency_status (\d+) offset (-?\d+) phase_status (\d+) pps_status (\d+) (\w+)rNrvrT�frequency_statusrWryrU� phase_statusr}� pps_statusrVr��rXrYr[r�r�s rr�z*PTPLogParser._parse_dpll_component_message2s������ �r�t{�|�|�� � -�"'�+�+�a�.�.�F�;� �),�U�[�[��^�^�)<�)<�F�%� &�"�5�;�;�q�>�>�2�2�F�8� �%(����Q���%8�%8�F�>� "�#&�u�{�{�1�~�~�#6�#6�F�<� �#�k�k�!�n�n�F�7�O�� rc�,�i}tjd|��}|rz|�d��|d<t|�d����|d<t|�d����|d<|�d��|d <|S) zParse GNSS component messagesz*(\w+) gnss_status (\d+) offset (\d+) (\w+)rNrvrTr�rWryrUr�r�r�s rr�z*PTPLogParser._parse_gnss_component_messageBs������ �G��Q�Q�� � -�"'�+�+�a�.�.�F�;� �$'�� � �A���$7�$7�F�=� !�"�5�;�;�q�>�>�2�2�F�8� �#�k�k�!�n�n�F�7�O�� rc��i}tjd|��}|r0|�d��|d<|�d��|d<|S)zParse GM (Grandmaster) messagesz(\w+) T-GM-STATUS (\w+)rNrvrT� gm_statusr�r�s rr�zPTPLogParser._parse_gm_messagePsP����� �4�g�>�>�� � 1�"'�+�+�a�.�.�F�;� �"'�+�+�a�.�.�F�;� �� rr1�query� time_range� log_levelc������|}|r#|�|����fd�|D��}�r�fd�|D��}|r"|�����fd�|D��}|S)z!Search logs for specific patternsc�*��g|]}|j�k� |��Sr�r )r:�log� cutoff_times �rr<z,PTPLogParser.search_logs.<locals>.<listcomp>cs%���Z�Z�Z�S�S�]�k�=Y�=Y�S�=Y�=Y�=Yrc�*��g|]}|j�k� |��Sr)r)r:r�r�s �rr<z,PTPLogParser.search_logs.<locals>.<listcomp>gs%���T�T�T�S�S�Y�)�=S�=S�S�=S�=S�=Src���g|]Q}�|j���vs2t�fd�|j���D�����O|��RS)c3�^�K�|]'}�t|�����vV��(dS�N)rrk)r:�v� query_lowers �r� <genexpr>z6PTPLogParser.search_logs.<locals>.<listcomp>.<genexpr>os6�����W�W��{�c�!�f�f�l�l�n�n�4�W�W�W�W�W�Wr)rrk�anyr�values)r:r�r�s �rr<z,PTPLogParser.search_logs.<locals>.<listcomp>lso��������#�+�"3�"3�"5�"5�5�5��W�W�W�W�c�o�>T�>T�>V�>V�W�W�W�W�W�6��5�5�5r)�_get_cutoff_timerk)r)r1r�r�r�� filtered_logsr�r�s ` @@r� search_logszPTPLogParser.search_logs\s������� � � [��/�/� �;�;�K�Z�Z�Z�Z�M�Z�Z�Z�M� � U�T�T�T�T�M�T�T�T�M� � ��+�+�-�-�K�����,����M� �rc�@�tj��}|dkr|td���z S|dkr|td���z S|dkr|td���z S|�d��r�d d l}|jd |��}|r�t |�d����}|�d ��}|d kr|t|���z S|dkr|t|���z S|dkr|t|���z S|td���z S)z*Get cutoff time based on time range string� last_hourrN)�hours�last_day)�days� last_week)�weeks�last_rNzlast_(\d+)([mhd])rT�m)�minutes�h�d)rr]r � startswithrXr_r�r[)r)r�r]rXr_�amount�units rr�zPTPLogParser._get_cutoff_timetsF���l�n�n�� �� $� $����+�+�+�+� +� �:� %� %����*�*�*�*� *� �;� &� &����+�+�+�+� +� � "� "�7� +� +� 8� �I�I�I��B�H�1�:�>�>�E�� 8��U�[�[��^�^�,�,���{�{�1�~�~���3�;�;���6�!:�!:�!:�:�:��S�[�[����!8�!8�!8�8�8��S�[�[����!7�!7�!7�7�7��Y�Q�'�'�'�'�'rc��dddddd�}d�|D��}|rWt|d����}|j�dd��|d<|j�d ��|d <|j|d <d �|D��}|rLt|d ����}|j�d ��|d <|j�d��|d<|S)z)Extract grandmaster information from logsrRN)rxrv� last_seenryr�c�(�g|]}|jdk� |��S)r#�r�r:r�s rr<z9PTPLogParser.extract_grandmaster_info.<locals>.<listcomp>�s$��@�@�@�3�#�-�4�*?�*?�3�*?�*?�*?rc��|jSr�r���xs r�<lambda>z7PTPLogParser.extract_grandmaster_info.<locals>.<lambda>�s��1�;�r��keyr�rxrvr�c�(�g|]}|jdk� |��S)rr�r�s rr<z9PTPLogParser.extract_grandmaster_info.<locals>.<listcomp>�s$��J�J�J��s�}� �/I�/I��/I�/I�/Irc��|jSr�r�r�s rr�z7PTPLogParser.extract_grandmaster_info.<locals>.<lambda>�s��Q�[�rryr���maxr�getr )r)r1�gm_info�gm_logs� latest_gm� phc2sys_logs�latest_phc2syss r�extract_grandmaster_infoz%PTPLogParser.extract_grandmaster_info�s�� �����  � ��A�@�$�@�@�@�� � 7��G�)>�)>�?�?�?�I� )� 5� 9� 9�+�y� Q� Q�G�H� �#,�#8�#<�#<�[�#I�#I�G�K� �#,�#6�G�K� �K�J�t�J�J�J� � � O� ��3H�3H�I�I�I�N� .� :� >� >�x� H� H�G�H� �#1�#=�#A�#A�+�#N�#N�G�K� ��rc��dddddd�}d�|D��}|rqt|d����}|j}|�dd��d k|d <|�d d��|d <|�d ��|d<|j|d<d�|D��}|r4t|d����}|j�dd��dk|d<|S)z(Extract synchronization status from logsFN)� dpll_locked�gnss_available�offset_in_range� last_offset� last_updatec�Z�g|](}d|j���v�d|jv�&|��)S)r!�decision)rrkrr�s rr<z4PTPLogParser.extract_sync_status.<locals>.<listcomp>�s?��j�j�j�S�F�c�m�6I�6I�6K�6K�,K�,K�PZ�^a�^i�Pi�Pi�S�Pi�Pi�Pirc��|jSr�r�r�s rr�z2PTPLogParser.extract_sync_status.<locals>.<lambda>����q�{�rr�rxrrWr�r{r�ryr�r�c�H�g|]}d|j���v�|�� S)r")rrkr�s rr<z4PTPLogParser.extract_sync_status.<locals>.<listcomp>�s0��L�L�L�S�F�c�m�6I�6I�6K�6K�,K�,K�S�,K�,K�,Krc��|jSr�r�r�s rr�z2PTPLogParser.extract_sync_status.<locals>.<lambda>�r�rr�r�r�)r)r1� sync_status� dpll_logs� latest_dpllrr� gnss_logs� latest_gnsss r�extract_sync_statusz PTPLogParser.extract_sync_status�s��!�#�$���  � � �k�j�D�j�j�j� � � ?��i�-B�-B�C�C�C�K� �,�F�)/���H�a�)@�)@�A�)E�K� � &�-3�Z�Z� �5�-I�-I�K�)� *�)/���H�)=�)=�K� � &�)4�)>�K� � &�M�L�D�L�L�L� � � ^��i�-B�-B�C�C�C�K�,7�,C�,G�,G� �WX�,Y�,Y�\]�,]�K�(� )��rc��dgggd�}d�|D��}|D]>}d|j�dd�����vr d|jd�|d<�?|S) z-Extract clock hierarchy information from logsN)� grandmaster�boundary_clocks�ordinary_clocks�transparent_clocksc�:�g|]}|jdk� d|jv�|��S)r�selected)rrr�s rr<z8PTPLogParser.extract_clock_hierarchy.<locals>.<listcomp>�s2��d�d�d�c�S�]�g�-E�-E�*�X[�Xc�Jc�Jc�c�Jc�Jc�Jcrrr���active)rxr�)rr�rkr )r)r1� hierarchy� ptp4l_logsr�s r�extract_clock_hierarchyz$PTPLogParser.extract_clock_hierarchy�s��� �!�!�"$�  � � �e�d�T�d�d�d� �� � �C���� 3� 3�4D�b� I� I� O� O� Q� Q�Q�Q�&�!$��,�,� �-�(�� �r)Nr+N)NN)rrrrr*rr�rr rLr9rrr`rmrnrorprbr�r�r�r�r�r�r�rr�r�r�r rrrrrs�������)�)� � � �"#�#�C�#�s�#�RU�#�ae�fn�ao�#�#�#�#�J3 �C�3 �H�3 �3 �3 �3 �j�S��S��T�#�s�(�^����� !�3�!�4��S��>�!�!�!�!�F�3��4��S��>����� �C� �D��c��N� � � � � �C� �D��c��N� � � � �������S�RU�X������& �c� �d�3��8�n� � � � ��S��T�#�s�(�^�����8�C��D��c��N�����"�S��T�#�s�(�^����� �S� �T�#�s�(�^� � � � � �� ��c�3�h�� � � � ����X���s����_b��nr�s{�n|�����0(�3�(�8�(�(�(�(�6�T�(�^���S�#�X������6��X���4��S��>�����8�D��N��t�C��H�~������rr)r�json�loggingrXr>�typingrrrrrrr � dataclassesr � getLoggerrrFr rrrr�<module>rs ���� � � � ����� � � � �����3�3�3�3�3�3�3�3�3�3�3�3�3�3�(�(�(�(�(�(�(�(�!�!�!�!�!�!� �� �8� $� $�� � � � � � � � � �� ����������r

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/aneeshkp/ptp-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server