
                             S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJ	r	  SSK
Jr  SS	KJr  SS
KJr  SSKJr  SSKJr  SrSrSrSrSrSrSrSrSrSrSS/rSr " S S\R>                  5      r g)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                   N    \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rSrg)VMStatusTroubleshooterJ   zCheck VM status.

Performance cpu, memory, disk status checking.

Attributes:
  project: The project object.
  zone: str, the zone name.
  instance: The instance object.
c                 V   Xl         X l        X0l        [        R                  " [
        [        5      U l        [        R                  " [
        [        5      U l	        [        R                  " [        [        5      U l        [        R                  " [        [        5      U l        0 U l        g 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__VMStatusTroubleshooter.__init__U   s    LIM!334O4NPD"44#%?AD001I1GID112J2HJDDK    c                 Z   [         R                  R                  S5        SnSnSn [        R                  " UUSUS9nU(       + U l        U R
                  (       a  g [        R                  " U R                  R                  [        5        g ! [        R                   a
    SU l         N_f = f)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_prerequisite)VMStatusTroubleshooter.check_prerequisitec   s    JJ34
CC	  F$"11	 o
 $32d  &&t||'8'8.I -- $#d$s   #B B*)B*c                     g r    r$   s    r%   cleanup_resources(VMStatusTroubleshooter.cleanup_resources{   s    
r(   c                    U R                   (       a  g U R                  5         U R                  5         U R                  5         [        R
                  R                  SR                  [        U R                  5      5      5        U R                  R                  5        H"  n[        R
                  R                  U5        M$     g )NzVM status: {0} issue(s) found.
)r1   _CheckVMStatus_CheckCpuStatus_CheckDiskStatusr   r.   r/   formatlenr#   values)r$   r*   s     r%   troubleshoot#VMStatusTroubleshooter.troubleshoot~   s    JJ7>>DKK  ;;%%'	jjw (r(   c                     U R                   R                  U R                  R                  R                  R
                  :w  a  [        U R                  S'   g g )N	vm_status)r   r.   r"   InstanceStatusValueValuesEnumRUNNINGVM_STATUS_MESSAGEr#   r>   s    r%   rB   %VMStatusTroubleshooter._CheckVMStatus   s@    }}t33<<RRZZZ!2dkk+ [r(   c                 `    U R                  5       nU[        :  a  [        U R                  S'   gg)zCheck cpu utilization.cpuN)_GetCpuUtilizationCPU_THRESHOLDCPU_MESSAGEr#   )r$   cpu_utilizatians     r%   rC   &VMStatusTroubleshooter._CheckCpuStatus   s+    --/O&&dkk% 'r(   c                    U R                  [        5      nU R                  R                  R	                  US9nUR
                  (       a7  UR
                  S   R                  n[        S U 5       5      [        U5      -  $ g)z.Get CPU utilization from Cloud Monitoring API.)requestr   c              3   L   #    U  H  oR                   R                  v   M     g 7fr   )valuedoubleValue).0points     r%   	<genexpr><VMStatusTroubleshooter._GetCpuUtilization.<locals>.<genexpr>   s     =fU((fs   "$g        )	_CreateTimeSeriesListRequestCPU_METRICSr   projects_timeSeriesList
timeSeriespointssumrF   )r$   rY   responserf   s       r%   rS   )VMStatusTroubleshooter._GetCpuUtilization   sm    //<G%%99>>w>OH""1%,,f=f==FKKr(   c                    [         R                  " U R                  U R                  U R                  R
                  U R                  R
                  U R                  5      n[         R                  " [        U5      (       aD  [        R                  U R                  R                  S   R                  5      U R                  S'   g g )Nr   disk)r
   GetSerialConsoleLogr!   r"   r   r4   r   r   SearchPatternErrorInLogDISK_ERROR_PATTERNDISK_MESSAGErE   diskssourcer#   )r$   sc_logs     r%   rD   'VMStatusTroubleshooter._CheckDiskStatus   s    %99T114==3E3E499&F  778J8>@ @(//0C0CA0F0M0MNdkk&@r(   c                 .   [         R                   R                  [         R                  R                  5      n[        R
                  " 5       nUR                  U5        [        R
                  " 5       nUR                  U[         R                  " SS9-
  5        U R                  R                  SR                  U R                  R                  S9[        R                  XR                  R                  S9UR                  5       UR                  5       S9$ )zCreate 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)r4   filterinterval_endTimeinterval_startTime)datetimenowtimezoneutcr   	TimestampFromDatetime	timedeltar   'MonitoringProjectsTimeSeriesListRequestrE   r   r4   FILTER_TEMPLATEr   ToJsonString)r$   rv   current_timetp_proto_end_timetp_proto_start_times        r%   ra   3VMStatusTroubleshooter._CreateTimeSeriesListRequest   s     $$(():):)>)>?L%//1""<0'113$$\%-%7%7%D&E F""JJ!((1B1B(C%%%]]5G5G & I*779.;;= K ? ?r(   )	r!   r"   r   r#   r   r   r   r1   r   N)__name__
__module____qualname____firstlineno____doc__r&   r:   r?   rH   rB   rC   rS   rD   ra   __static_attributes__r=   r(   r%   r   r   J   s6    J0
 3'O?r(   r   )!r   
__future__r   r   r   r{   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    r5   rO   rb   rU   r   rT   rn   ro   SshTroubleshooterr   r=   r(   r%   <module>r      s    3 &  '  2 6 , A G # 2* ! $  ,L  @L 5   - h q?/AA q?r(   