Skip to main content
Glama

PTP MCP Server

by aneeshkp
ptp_tools.cpython-311.pyc21.6 kB
� H�|hM���dZddlZddlZddlmZmZmZmZddlmZm Z ddl m Z ddl m Z ddlmZddlmZeje��ZGd �d ��ZdS) z< PTP Tools - Implementation of MCP tools for PTP monitoring �N)�Dict�List�Optional�Any)�datetime� timedelta)�PTPConfigParser)� PTPLogParser)�PTPModel)�PTPQueryEnginec��eZdZdZd�Zdeeefdeeeffd�Zdeeefdeeeffd�Z deeefdeeeffd�Z deeefdeeeffd�Z deeefdeeeffd �Z deeefdeeeffd �Z deeefdeeeffd �Zdeeefdeeeffd �Zd S)�PTPToolszImplementation of PTP MCP toolsc��t��|_t��|_t ��|_t ��|_dS)N)r � config_parserr � log_parserr �modelr � query_engine)�selfs �5/home/aputtur/Ai Projects/ptp-mcp-server/ptp_tools.py�__init__zPTPTools.__init__s:��,�.�.���&�.�.����Z�Z�� �*�,�,������ arguments�returnc ��dK� |�dd��}|j�|���d{V��}|j�|��}|j�|dd��}|j�|��}d|j|j|j j |j |j |j |j|j|j|jd� |||d�S#t$$rG}t&�d t+|������d t+|��dd �cYd}~Sd}~wwxYw) z,Get PTP configuration from OpenShift cluster� namespace� openshift-ptpN�itemsrT) �namer� clock_type�domain� priorities� clock_class�sync_intervals� thresholds�profiles�recommendations)�success� configuration� validation�itu_compliance�raw_datazError getting PTP config: F)r'�errorr()�getr�get_ptp_configsr�create_ptp_configuration�validate_config�validate_itu_t_compliancerrr�valuer r!r"r#r$r%r&� Exception�loggerr,�str)rrr� config_data� ptp_configr)�itu_validation�es r�get_ptp_configzPTPTools.get_ptp_configsy����( �!� � �k�?�C�C�I�!%� 2� B� B�9� M� M�M�M�M�M�M�M�K���<�<�[�I�I�J��+�;�;�K��<P�QR�<S�T�T�J�"�Z�A�A�*�M�M�N� �&�O�!+�!5�",�"7�"=�(�/�",�"7�#-�#9�&0�&?�",�"7� *� 3�'1�'A� "� "�)�"0�'�!�� ��&� � � � �L�L�>�c�!�f�f�>�>� ?� ?� ?� ��Q���!%��� � � � � � ����� ���s�CC� D/�(<D*�$D/�*D/c ��bK� |�dd��}|�dd��}|�d��}|j�|||���d{V��}|j�|��}|j�|��}|j�|��}dt |��|||d�|d d�D��d �S#t$rG} t� d t| ������d t| ��d d�cYd} ~ Sd} ~ wwxYw)z#Get PTP logs from OpenShift clusterrr�linesi��sinceNTc�t�g|]5}|j���|j|j|j|jd���6S�)� timestamp� component�level�message� parsed_data�r@� isoformatrArBrCrD��.0�logs r� <listcomp>z)PTPTools.get_ptp_logs.<locals>.<listcomp>\sV�� � � �� &)�]�%<�%<�%>�%>�%(�]�!$��#&�;�'*�� �� � � ri����)r'� logs_count� grandmaster� sync_status�clock_hierarchy� log_entrieszError getting PTP logs: Fr)r'r,rK) r-r� get_ptp_logs�extract_grandmaster_info�extract_sync_status�extract_clock_hierarchy�lenr3r4r,r5) rrrr<r=�logs�gm_inforMrNr9s rrPzPTPTools.get_ptp_logsGsu����% �!� � �k�?�C�C�I��M�M�'�4�0�0�E��M�M�'�*�*�E���5�5�i���N�N�N�N�N�N�N�N�D��o�>�>�t�D�D�G��/�=�=�d�C�C�K�"�o�E�E�d�K�K�O� �!�$�i�i�&�*�#2� � � $�D�E�E�{� � � � �� ��$� � � � �L�L�<�C��F�F�<�<� =� =� =� ��Q������ � � � � � ����� ���s�CC� D.�'<D)�#D.�)D.c��K� |�dd��}|�d��}|�d��}|j����d{V��}|j�||||��}d|||t |��t |��d�|D��d�S#t $rG}t �d t|������d t|��gd �cYd}~Sd}~wwxYw) z%Search PTP logs for specific patterns�query�� time_range� log_levelNTc�t�g|]5}|j���|j|j|j|jd���6Sr?rErGs rrJz(PTPTools.search_logs.<locals>.<listcomp>�sV�� � � �� &)�]�%<�%<�%>�%>�%(�]�!$��#&�;�'*�� �� � � r)r'rXrZr[� total_logs� matching_logs�resultszError searching logs: F)r'r,r_) r-rrP� search_logsrTr3r4r,r5)rrrXrZr[rU� filtered_logsr9s rr`zPTPTools.search_logspsM����$ ��M�M�'�2�.�.�E�"���|�4�4�J�!� � �k�2�2�I���5�5�7�7�7�7�7�7�7�7�D�!�O�7�7��e�Z�QZ�[�[�M� ��(�&�!�$�i�i�!$�]�!3�!3� � � -� � � ��� ��&� � � � �L�L�:�#�a�&�&�:�:� ;� ;� ;� ��Q������ � � � � � ����� ���s�B)B.�. C?�8<C:�4C?�:C?c��K� |�dd��}|j����d{V��}|j�|��}|j����d{V��}|j�|��}d||j|j j |j d�d�}|rD|j� |��}||d<|j� ||��} | j |d<|S#t$rI} t�d t#| ������dt#| ��d d id �cYd} ~ Sd} ~ wwxYw) zGet current grandmaster status�detailedFNT)r rr")r'rL�configuration_contextrM� bmca_rolez"Error getting grandmaster status: �status�unknown)r'r,rL)r-rrPrQrr.rr/r rr2r"rR�analyze_bmca_stater3r4r,r5) rrrcrUrVr6r7�resultrMrer9s r�get_grandmaster_statuszPTPTools.get_grandmaster_status�s�����& � �}�}�Z��7�7�H���5�5�7�7�7�7�7�7�7�7�D��o�>�>�t�D�D�G�!%� 2� B� B� D� D�D�D�D�D�D�D�K���<�<�[�I�I�J� �&�(�/�",�"7�"=�#-�#9�*�*���F�� 6�"�o�A�A�$�G�G� �(3��}�%�!�J�9�9�*�d�K�K� �&/�o��{�#��M��� � � � �L�L�F�c�!�f�f�F�F� G� G� G� ��Q��� (�)�4��� � � � � � ����� ���s�C-C2�2 E�<>E�:E�Ec��K� |�dd��}|�dd��}|j����d{V��}|j����d{V��}|j�|��}|j�|��}|j�||��}d||j |j j |j |j d�d�} |r|j� |��} | | d<|r%|j�||��} | j | d<| S#t$rJ} t �d t%| ������d t%| ��d d d �d �cYd} ~ Sd} ~ wwxYw) z"Analyze PTP synchronization status�include_offsetsT� include_bmcaN)rr r")r'rM�model_sync_statusr(� offset_trendrezError analyzing sync status: F)� dpll_locked�offset_in_range)r'r,rM)r-rrPrr.rr/rR�analyze_sync_statusr2rr r"�get_offset_trendrhr3r4r,r5) rrrlrmrUr6r7rMrnrirorer9s rrrzPTPTools.analyze_sync_status�s�����, �'�m�m�,=�t�D�D�O�$�=�=���>�>�L���5�5�7�7�7�7�7�7�7�7�D� $� 2� B� B� D� D�D�D�D�D�D�D�K���<�<�[�I�I�J��/�=�=�d�C�C�K�!%� � >� >�z�4� P� P� � �*�%6�%<�",�"7�"=�(�/�#-�#9�"�"� � �F�� 6�#�z�:�:�4�@�@� �)5��~�&�� 6� �J�9�9�*�d�K�K� �&/�o��{�#��M��� � � � �L�L�A��Q���A�A� B� B� B� ��Q���/4��O�O��� � � � � � ����� ���s�D&D+�+ E?�5?E:�4E?�:E?c ��~K� |�dd��}|�dd��}|j����d{V��}|j����d{V��}|j�|��}|j�||��}|j�|��}|� |��d|d�} |r�g} |j D]u} | �di��} | �di��} | � ��D]2\}}| � ||�dd ��|d ����3�v| | d <|r |j | d <| S#t$rG}t�d t#|������d t#|��id�cYd}~Sd}~wwxYw)zGet current clock hierarchy� include_portsT�include_prioritiesN)r'� hierarchy� ptp4lConf� interfaces� masterOnlyF)r� master_only�config�portsr!zError getting clock hierarchy: )r'r,rw)r-rrPrr.rr/�get_clock_hierarchyrS�updater%r�appendr!r3r4r,r5)rrrurvrUr6r7rw� log_hierarchyrir}�profile� ptp4l_confry�interface_name�interface_configr9s rr~zPTPTools.get_clock_hierarchy�s4����. �%�M�M�/�4�@�@�M�!*���/C�T�!J�!J� ���5�5�7�7�7�7�7�7�7�7�D� $� 2� B� B� D� D�D�D�D�D�D�D�K���<�<�[�I�I�J�� �6�6�z�4�H�H�I�!�O�C�C�D�I�I�M� � � �]� +� +� +� �&���F� � (���)�2���G�!(���[�"�!=�!=�J�!+��� �b�!A�!A�J�<F�<L�<L�<N�<N���8��(8�� � �$2�+;�+?�+?� �e�+T�+T�&6�&�&������ #(��w��!� =�'1�'<��|�$��M��� � � � �L�L�C�3�q�6�6�C�C� D� D� D� ��Q������ � � � � � ����� ���s�E&E+�+ F<�5<F7�1F<�7F<c��`K� |�dd��}|�dd��}|�dd��}ddid�}|r� |j����d{V��}|j�|��}|j�|dd ��}|j�|��} |d |d |d | d d�|dd<n2#t$r%} dt| ��d�|dd<Yd} ~ nd} ~ wwxYw|r� |j � ���d{V��} |j � | ��} | �dd��| �dd��| �dd��| �d��d�|dd<n1#t$r$} dt| ��i|dd<Yd} ~ nd} ~ wwxYw|r� |j � ���d{V��} d�| D��} d�| D��}t| ��t| ��t|��d�| dd�D��d�|dd<n1#t$r$} dt| ��i|dd<Yd} ~ nd} ~ wwxYw|d�di���d d��}|d�di���dd��}|d�di���d d ��d k}|r |r|rd!|d"<n|sd#|d"<n |sd$|d"<nd$|d"<|S#t$rG} t�d%t| ������dt| ��dd&�cYd} ~ Sd} ~ wwxYw)'zComprehensive PTP health check� check_configT� check_sync� check_logsrg)r'�overall_status�checksNrr�valid�errors�warnings� compliant)r�r�r�� itu_compliantr�r(F)r�r,rprq�gnss_available� last_offset)rprqr�r��synchronizationr,c�(�g|]}|jdk� |��S)r,�rBrGs rrJz-PTPTools.check_ptp_health.<locals>.<listcomp>\s$��!N�!N�!N�#���g�9M�9M�#�9M�9M�9Mrc�(�g|]}|jdk� |��S)�warningr�rGs rrJz-PTPTools.check_ptp_health.<locals>.<listcomp>]s$��#R�#R�#R�C�3�9� �;Q�;Q�C�;Q�;Q�;Qrc�\�g|])}|j���|j|jd���*S))r@rArC)r@rFrArCrGs rrJz-PTPTools.check_ptp_health.<locals>.<listcomp>csL��*�*�*� !$� .1�]�-D�-D�-F�-F�-0�]�+.�;���*�*�*ri����)r]� error_count� warning_count� recent_errorsrUr��healthyr��criticalr�zError checking PTP health: )r'r,r�)r-rr.rr/r0r1r3r5rrPrRrTr4r,)rrr�r�r�� health_resultr6r7�config_validationr8r9rUrM� error_logs� warning_logs�config_healthy� sync_healthy� logs_healthys r�check_ptp_healthzPTPTools.check_ptp_health$s�����a �$�=�=���>�>�L�"���|�T�:�:�J�"���|�T�:�:�J� �"+����M�� ��(,�(:�(J�(J�(L�(L�"L�"L�"L�"L�"L�"L�K�!%��!D�!D�[�!Q�!Q�J�(,�(:�(J�(J�;�W^�K_�`a�Kb�(c�(c�%�%)�Z�%I�%I�*�%U�%U�N�"3�7�!;�"3�H�"=�$5�j�$A�)7� �)D� @�@�M�(�+�O�<�<�� !����!&�!$�Q���@�@�M�(�+�O�<�<�<�<�<�<���������� � �!%��!=�!=�!?�!?�?�?�?�?�?�?�D�"&�/�"E�"E�d�"K�"K�K�(3���}�e�'L�'L�+6�?�?�;L�e�+T�+T�*5�/�/�:J�E�*R�*R�'2���}�'E�'E� B�B�M�(�+�,=�>�>�� !������Q���B�M�(�+�,=�>�>�>�>�>�>��������� � ��!%��!=�!=�!?�!?�?�?�?�?�?�?�D�"O�!N��!N�!N�!N�J�#R�#R�4�#R�#R�#R�L�'*�$�i�i�'*�:���),�\�):�):�*�*� (2�#�$�$�'7� *�*�*� 7� 7�M�(�+�F�3�3��!������Q���7�M�(�+�F�3�3�3�3�3�3��������� +�8�4�8�8��"�M�M�Q�Q�RY�[`�a�a�N�(��2�6�6�7H�"�M�M�Q�Q�R_�af�g�g�L�(��2�6�6�v�r�B�B�F�F�}�VW�X�X�\]�]�L�� <�,� <�<� <�2;� �.�/�/�#� <�2<� �.�/�/�!� <�2;� �.�/�/�2;� �.�/� � ��� � � � �L�L�?�s�1�v�v�?�?� @� @� @� ��Q���"+��� � � � � � ����� ���s��A M�B!C1�0M�1 D �;D�M�D � M�&BF?�>M�? G-� G(�#M�(G-�-M�3BI4�3M�4 J"�>J�M�J"�"B9M� N-�&<N(�"N-�(N-c���K� |�dd��}|�d��}|sdd|j���d�S|j�||��}i}|ddvr� |j����d {V��}|j�|��}|j|j j |j |j |j |jd �|d <n+#t$r}d t!|��i|d <Yd }~nd }~wwxYw|dd vr� |j����d {V��} |j�| ��|d<|j�| ��|d<|j�| ��|d<|j�| |�d����|d<n+#t$r}d t!|��i|d<Yd }~nd }~wwxYw|ddkr� |j����d {V��}|j�|��}|j�|��|d<n+#t$r}d t!|��i|d<Yd }~nd }~wwxYw|ddkrQ |�dddd����d {V��} | |d<n+#t$r}d t!|��i|d<Yd }~nd }~wwxYw|j�||��} d||| |d�S#t$rs}t4�dt!|������dt!|��|�dd��|j���d�cYd }~Sd }~wwxYw)z4Natural language query interface for PTP information�questionrY�contextFzNo question provided)r'r,� suggestions� query_type)rLr(rMrN� health_checkN)rrr r!r"r#r(r,)rLrMrNro� bmca_staterLrMrNrZrorUr*r�T)r�r�r��health)r'r�� query_info�response�datazError processing PTP query: )r'r,r�r�)r-r�suggest_queries� parse_queryrr.rr/rrr2r r!r"r#r3r5rrPrQrRrSrsr1r��generate_responser4r,) rrr�r�r�r�r6r7r9rUr�r�s r� query_ptpzPTPTools.query_ptp�sa����O � �}�}�Z��4�4�H��m�m�I�.�.�G�� �$�3�#'�#4�#D�#D�#F�#F�����*�6�6�x��I�I�J��D��,�'�+}�}�}� >�(,�(:�(J�(J�(L�(L�"L�"L�"L�"L�"L�"L�K�!%��!D�!D�[�!Q�!Q�J� *��&0�&;�&A�",�"3�&0�&;�'1�'=�*4�*C� -�-�D��)�)��!�>�>�>�-4�c�!�f�f�,=�D��)�)�)�)�)�)�����>�����,�'�+z�z�z�5�!%��!=�!=�!?�!?�?�?�?�?�?�?�D�*.�/�*R�*R�SW�*X�*X�D��'�*.�/�*M�*M�d�*S�*S�D��'�.2�o�.U�.U�VZ�.[�.[�D�*�+�+/�:�+F�+F�t�Z�^�^�\h�Mi�Mi�+j�+j�D��(�(�� �5�5�5�$+�S��V�V�#4�D��L�L�L�L�L�L�����5�����,�'�+;�;�;�?�(,�(:�(J�(J�(L�(L�"L�"L�"L�"L�"L�"L�K�!%��!D�!D�[�!Q�!Q�J�-1�Z�-Q�-Q�R\�-]�-]�D�)�*�*�� �?�?�?�.5�s�1�v�v�->�D�)�*�*�*�*�*�*�����?�����,�'�>�9�9�7�*.�*?�*?�QU�ei�y}�@~�@~�*�*�$�$�$�$�$�$�M�%2�D��N�N�� �7�7�7�&-�s�1�v�v�%6�D��N�N�N�N�N�N�����7�����(�:�:�:�t�L�L�H� �$�(�$�� �� ��� � � � �L�L�@��A���@�@� A� A� A� ��Q���%�M�M�*�b�9�9�#�0�@�@�B�B� �� � � � � � ����� ���s��A K7�'K7�6A(C�K7� D�)D�=K7�D� K7�B'F=�<K7�= G%�G �K7� G%�%K7�5AI � K7� I4�I/�*K7�/I4�4K7�$J)�(K7�) K�3K �K7� K�%K7�7 M4�A(M/�)M4�/M4N)�__name__� __module__� __qualname__�__doc__rrr5rr:rPr`rjrrr~r�r��rrrrs�������)�)�-�-�-� *�d�3��8�n�*��c�3�h��*�*�*�*�X'�D��c��N�'�t�C��H�~�'�'�'�'�R&�4��S��>�&�d�3��8�n�&�&�&�&�P(�d�3��8�n�(��c�SV�h��(�(�(�(�T.�4��S��>�.�d�3�PS�8�n�.�.�.�.�`0�4��S��>�0�d�3�PS�8�n�0�0�0�0�dc��S�#�X��c�4��S��>�c�c�c�c�JQ��c�3�h��Q�D��c��N�Q�Q�Q�Q�Q�Qrr)r��json�logging�typingrrrrrr�ptp_config_parserr �ptp_log_parserr � ptp_modelr �ptp_query_enginer � getLoggerr�r4rr�rr�<module>r�s����� � � � �����,�,�,�,�,�,�,�,�,�,�,�,�(�(�(�(�(�(�(�(�-�-�-�-�-�-�'�'�'�'�'�'�������+�+�+�+�+�+� �� �8� $� $��H�H�H�H�H�H�H�H�H�Hr

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