parameters: (certificate_path)
return_statement: return check_result
docstring: Check whether the certificate is expired or invalid.
      :param certificate_path: path of certificate.
      output: dict, check result which include 'check status' and 'check information'.
docstring_summary: Check whether the certificate is expired or invalid.
      :param certificate_path: path of certificate.
      output: dict, check result which include 'check status' and 'check information'.
docstring_tokens: Check whether the certificate is expired or invalid . : param certificate_path : path of certificate . output : dict check result which include check status and check information .
function: def check_certificate(certificate_path):
      """
      Check whether the certificate is expired or invalid.
      :param certificate_path: path of certificate.
      output: dict, check result which include 'check status' and 'check information'.
      """
      check_result = {}
      certificate_warn_threshold = 365
      child = subprocess.Popen(['openssl', 'x509', '-in', certificate_path, '-noout', '-dates'],
                               shell=False, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
      sub_chan = child.communicate()
      if sub_chan[1] or not sub_chan[0]:
          check_result['status'] = 'fail'
      else:
          check_result['status'] = 'success'
          not_after = sub_chan[0].decode('utf-8').split('\n')[1].split('=')[1].strip()
          end_time = dateutil.parser.parse(not_after).replace(tzinfo=None)
          certificate_remaining_days = (end_time - datetime.now()).days
          if 0 < certificate_remaining_days < certificate_warn_threshold:
              check_result['level'] = 'warn'
              check_result['info'] = "the '{certificate}' has {certificate_remaining_days} days before out of date." \
                  .format(certificate=certificate_path,
                          certificate_remaining_days=certificate_remaining_days)
          elif certificate_remaining_days >= certificate_warn_threshold:
              check_result['level'] = 'info'
              check_result['info'] = "the '{certificate}' has {certificate_remaining_days} days before out of date." \
                  .format(certificate=certificate_path,
                          certificate_remaining_days=certificate_remaining_days)
          else:
              check_result['level'] = 'error'
              check_result['info'] = "the '{certificate}' is out of date.".format(certificate=certificate_path)
      return check_result
function_tokens: def check_certificate ( certificate_path ) : check_result = { } certificate_warn_threshold = 365 child = subprocess . Popen ( [ 'openssl' , 'x509' , '-in' , certificate_path , '-noout' , '-dates' ] , shell = False , stdout = subprocess . PIPE , stdin = subprocess . PIPE ) sub_chan = child . communicate ( ) if sub_chan [ 1 ] or not sub_chan [ 0 ] : check_result [ 'status' ] = 'fail' else : check_result [ 'status' ] = 'success' not_after = sub_chan [ 0 ] . decode ( 'utf-8' ) . split ( '\n' ) [ 1 ] . split ( '=' ) [ 1 ] . strip ( ) end_time = dateutil . parser . parse ( not_after ) . replace ( tzinfo = None ) certificate_remaining_days = ( end_time - datetime . now ( ) ) . days if 0 < certificate_remaining_days < certificate_warn_threshold : check_result [ 'level' ] = 'warn' check_result [ 'info' ] = "the '{certificate}' has {certificate_remaining_days} days before out of date." . format ( certificate = certificate_path , certificate_remaining_days = certificate_remaining_days ) elif certificate_remaining_days >= certificate_warn_threshold : check_result [ 'level' ] = 'info' check_result [ 'info' ] = "the '{certificate}' has {certificate_remaining_days} days before out of date." . format ( certificate = certificate_path , certificate_remaining_days = certificate_remaining_days ) else : check_result [ 'level' ] = 'error' check_result [ 'info' ] = "the '{certificate}' is out of date." . format ( certificate = certificate_path ) return check_result