[ SEA-GHOST MINI SHELL]

Path : /proc/2/root/var/lib/zabbix/
FILE UPLOADER :
Current File : //proc/2/root/var/lib/zabbix/jenkins_checks.py

#!/usr/bin/env python


from argparse import ArgumentParser
import jenkins
import json
from sys import exit

def get_opts():
    parser = ArgumentParser(
        usage='%(prog)s -s <Server name of Jenkins> -u <User> -t <Token> -a <monitor_system|monitor_job|discovery> -j <Job name> -m <queue|build_time|build_result>',
        description='This program is used to connect to Jenkins API and getting status of jobs and other metrics'
    )
    parser.add_argument(
        "-s", "--server",
    	action="store",
        dest="server",
        help="URL of Jenkins API",
        required=True,
    )
    parser.add_argument(
        "-u", "--user",
        action="store",
        dest="user",
        help="User name",
        required=True,
    )
    parser.add_argument(
        "-t", "--token",
        action="store",
        dest="token",
        help="Token for the user",
        required=True,
    )
    parser.add_argument(
        "-a", "--action",
        action="store",
        dest="action",
        choices=['monitor_job', 'discovery', 'monitor_system'],
        help="Need to choose one of actions",
        required=True,
    )
    parser.add_argument(
        "-j", "--job",
        action="store",
        dest="job",
        help="Job name to monitoring",
    )
    parser.add_argument(
        "-m", "--metric",
        action="store",
        dest="metric",
        help="Metric to monitoring",
    )


    args = parser.parse_args()
    args.server = "https://" + args.server
    return args

def discovering_jobs(api):
   data_output = {"data": [] }
   for job in api.get_all_jobs():
      if job["_class"] != "com.cloudbees.hudson.plugins.folder.Folder" and job["color"] != "disabled":
         data_output["data"].append({"{#JENKINS_JOB}": job["fullname"]})
   lld_zabbix = json.dumps(data_output)
   print(lld_zabbix)


def do_monitoring_jobs(api,job_name,metric_name):
   build_name = job_name
   if metric_name == "build_result":
      status = ''
      try:
         build_number = api.get_job_info(build_name)["lastBuild"]["number"]
      except (KeyError, TypeError) as err:
         print("1")
         exit(0)
      build = api.get_build_info(build_name,build_number)
      if build["building"]:
         status = "1"
      elif build["result"] == "SUCCESS":
          status = "1"
      else:
         status = build["result"]
      print(status)

   elif metric_name == "build_time":
      try:
         build_number = api.get_job_info(build_name)["lastBuild"]["number"]
      except (KeyError, TypeError) as err:
         print(err)
         exit(0)
      build_time = api.get_build_info(build_name,build_number)
      status = build_time["duration"]
      if status == 0:
         build_number = build_number - 1
         build_time = api.get_build_info(build_name,build_number)
         status = build_time["duration"]
      print(status)

   elif metric_name is not True:
      print("Please define metric name")


def do_monitoring_systems(api,metric_name):
   if metric_name == "queue":
      queue = api.get_queue_info()
      if not len(queue):
         print("0")
      else:
         queue_number = queue[0]["actions"][0]["causes"]
         print len(queue_number)

   elif metric_name is not True:
      print("Please define metric name")



def main():
   args = get_opts()
   jenkins_api = jenkins.Jenkins(url = args.server, username = args.user, password = args.token)

   if args.action == "discovery":
      discovering_jobs(jenkins_api)
   elif args.action == "monitor_job":
      do_monitoring_jobs(jenkins_api,args.job,args.metric)
   elif args.action == "monitor_system":
      do_monitoring_systems(jenkins_api,args.metric)


if __name__ == "__main__":
    main()

SEA-GHOST - SHELL CODING BY SEA-GHOST