
                             d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddl
mZ dd	lmZ dd
lmZ ddlmZ ddlmZ dZdZdZdZdZdZdZdZdZdZddgZdZ G d dej>                        Z y)z,Troubleshoot VPC setting for ssh connection.    )absolute_import)division)unicode_literalsN)timestamp_pb2)
enable_api)apis)ssh_troubleshooter)ssh_troubleshooter_utils)log)
console_io
monitoringv3computev1zmonitoring.googleapis.comzThe VM may not be running. Try restarting it. If this doesn't work, the VM may be in a panic state.
Help for restarting a VM: https://cloud.google.com/compute/docs/instances/stop-start-instance
z/compute.googleapis.com/instance/cpu/utilizationzVM CPU utilization is high, which may cause slow SSH connections. Stop your VM instance, increase the number of CPUs, and then restart it.
Help for stopping a VM: https://cloud.google.com/compute/docs/instances/stop-start-instance
zQmetric.type = "{metrics_type}" AND metric.label.instance_name = "{instance_name}"gGz?z&No usable temporary directory found inzNo space left on devicea  The VM may need additional disk space. Resize and then restart the VM, or run a startup script to free up space.
Disk: {0}
Help for resizing a boot disk: https://cloud.google.com/sdk/gcloud/reference/compute/disks/resize
Help for running a startup script: https://cloud.google.com/compute/docs/startupscript
Help for additional troubleshooting of full disks: https://cloud.google.com/compute/docs/troubleshooting/troubleshooting-disk-full-resize#filesystemc                   F    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zy)VMStatusTroubleshooterzCheck VM status.

  Performance cpu, memory, disk status checking.

  Attributes:
    project: The project object.
    zone: str, the zone name.
    instance: The instance object.
  c                 T   || _         || _        || _        t        j                  t
        t              | _        t        j                  t
        t              | _	        t        j                  t        t              | _        t        j                  t        t              | _        i | _        y N)projectzoneinstancer   GetClientInstance_API_MONITORING_CLIENT_NAME_API_MONITORING_VERSION_V3monitoring_clientGetMessagesModulemonitoring_message_API_COMPUTE_CLIENT_NAME_API_CLIENT_VERSION_V1compute_clientcompute_messageissues)selfr   r   r   s       Blib/googlecloudsdk/command_lib/compute/vm_status_troubleshooter.py__init__zVMStatusTroubleshooter.__init__U   s    DLDIDM!334O4NPD"44#%?AD001I1GID112J2HJDDK    c                 H   t         j                  j                  d       d}d}d}	 t        j                  ||d|      }| | _        | j
                  ry t        j                  | j                  j                  t               y # t        j                  $ r
 d| _        Y Xw xY w)Nz---- Checking VM status ----z6The Monitoring API is needed to check the VM's Status.zHIf not already enabled, is it OK to enable it and check the VM's Status?zTest skipped.T)messageprompt_stringcancel_on_nocancel_string)r   statusPrintr   PromptContinueskip_troubleshootOperationCancelledErrorr   EnableServiceIfDisabledr   nameMONITORING_API)r#   msgpromptcancelprompt_continues        r$   check_prerequisitez)VMStatusTroubleshooter.check_prerequisitec   s    JJ34
CC	  F$"11	 o
 $32d  &&t||'8'8.I -- $#d$s   !B B! B!c                      y r    r#   s    r$   cleanup_resourcesz(VMStatusTroubleshooter.cleanup_resources{   s    
r&   c                 |   | j                   ry | j                          | j                          | j                          t        j
                  j                  dj                  t        | j                                     | j                  j                         D ]!  }t        j
                  j                  |       # y )NzVM status: {0} issue(s) found.
)r/   _CheckVMStatus_CheckCpuStatus_CheckDiskStatusr   r,   r-   formatlenr"   values)r#   r(   s     r$   troubleshootz#VMStatusTroubleshooter.troubleshoot~   s    JJ7>>DKK  ;;%%'	jjw (r&   c                     | j                   j                  | j                  j                  j                  j
                  k7  rt        | j                  d<   y y )N	vm_status)r   r,   r!   InstanceStatusValueValuesEnumRUNNINGVM_STATUS_MESSAGEr"   r;   s    r$   r>   z%VMStatusTroubleshooter._CheckVMStatus   s@    }}t33<<RRZZZ!2dkk+ [r&   c                 ^    | j                         }|t        kD  rt        | j                  d<   yy)zCheck cpu utilization.cpuN)_GetCpuUtilizationCPU_THRESHOLDCPU_MESSAGEr"   )r#   cpu_utilizatians     r$   r?   z&VMStatusTroubleshooter._CheckCpuStatus   s+    --/O&&dkk% 'r&   c                     | j                  t              }| j                  j                  j	                  |      }|j
                  r7|j
                  d   j                  }t        d |D              t        |      z  S y)z.Get CPU utilization from Cloud Monitoring API.)requestr   c              3   H   K   | ]  }|j                   j                    y wr   )valuedoubleValue).0points     r$   	<genexpr>z<VMStatusTroubleshooter._GetCpuUtilization.<locals>.<genexpr>   s     =fU((fs    "g        )	_CreateTimeSeriesListRequestCPU_METRICSr   projects_timeSeriesList
timeSeriespointssumrB   )r#   rR   responser^   s       r$   rM   z)VMStatusTroubleshooter._GetCpuUtilization   sm    //<G%%99>>w>OH""1%,,f=f==FKKr&   c                 ~   t        j                  | j                  | j                  | j                  j
                  | j                  j
                  | j                        }t        j                  t        |      rDt        j                  | j                  j                  d   j                        | j                  d<   y y )Nr   disk)r
   GetSerialConsoleLogr    r!   r   r2   r   r   SearchPatternErrorInLogDISK_ERROR_PATTERNDISK_MESSAGErA   diskssourcer"   )r#   sc_logs     r$   r@   z'VMStatusTroubleshooter._CheckDiskStatus   s    %99T114==3E3E499&F  778J8>@(//0C0CA0F0M0MNdkk&@r&   c                 B   t         j                   j                  t         j                  j                        }t	        j
                         }|j                  |       t	        j
                         }|j                  |t        j                  d      z
         | j                  j                  dj                  | j                  j                        t        j                  || j                  j                        |j                         |j                               S )a  Create a MonitoringProjectsTimeSeriesListRequest.

    Args:
      metrics_type: str, https://cloud.google.com/monitoring/api/metrics

    Returns:
      MonitoringProjectsTimeSeriesListRequest, input message for
      ProjectsTimeSeriesService List method.
    iX  )secondszprojects/{project})r   )metrics_typeinstance_name)r2   filterinterval_endTimeinterval_startTime)datetimenowtimezoneutcr   	TimestampFromDatetime	timedeltar   'MonitoringProjectsTimeSeriesListRequestrA   r   r2   FILTER_TEMPLATEr   ToJsonString)r#   rl   current_timetp_proto_end_timetp_proto_start_times        r$   rY   z3VMStatusTroubleshooter._CreateTimeSeriesListRequest   s     $$(():):)>)>?L%//1""<0'113$$\%-%7%7%D&E F""JJ!((1B1B(C%%%T]]5G5G & I*779.;;= K ? ?r&   N)__name__
__module____qualname____doc__r%   r8   r<   rD   r>   r?   rM   r@   rY   r:   r&   r$   r   r   J   s6    J0
 3'O?r&   r   )!r   
__future__r   r   r   rq   cloudsdk.google.protobufr   googlecloudsdk.api_lib.servicesr   googlecloudsdk.api_lib.utilr   "googlecloudsdk.command_lib.computer	   r
   googlecloudsdk.corer   googlecloudsdk.core.consoler   r   r   r   r   r3   rJ   rZ   rO   ry   rN   re   rf   SshTroubleshooterr   r:   r&   r$   <module>r      s    3 &  '  2 6 , A G # 2* ! $  ,L  @L 5   - h q?/AA q?r&   