def display_monitor(self,id): try: os.system('clear') ltime = datetime.datetime.now() jcs.output(SNABB_V6_TEMPLATE %(ltime,id, self.lwaftr[id][0], self.lwaftr_ps[id][0], self.lwaftr[id][1], self.lwaftr_ps[id][1], self.lwaftr[id][2], self.lwaftr_ps[id][2], self.lwaftr[id][3], self.lwaftr_ps[id][3], self.lwaftr[id][4], self.lwaftr_ps[id][4], self.lwaftr[id][5], self.lwaftr_ps[id][5], self.lwaftr[id][6], self.lwaftr_ps[id][6], self.lwaftr[id][7], self.lwaftr_ps[id][7], self.lwaftr[id][8], self.lwaftr_ps[id][8])) jcs.output(SNABB_V4_TEMPLATE %(self.lwaftr_ifInDiscards[id], self.lwaftr_ifInDiscards_ps[id], self.breaths[id],self.breaths_ps[id], self.lwaftr[id][9], self.lwaftr_ps[id][9], self.lwaftr[id][10], self.lwaftr_ps[id][10], self.lwaftr[id][11], self.lwaftr_ps[id][11], self.lwaftr[id][12], self.lwaftr_ps[id][12], self.lwaftr[id][13], self.lwaftr_ps[id][13], self.lwaftr[id][14], self.lwaftr_ps[id][14], self.lwaftr[id][15], self.lwaftr_ps[id][15], self.lwaftr[id][16], self.lwaftr_ps[id][16], self.lwaftr[id][17], self.lwaftr_ps[id][17])) except Exception as e: pass
def Main(argv): try: server = xmlrpclib.ServerProxy('http://127.0.0.1:9191', verbose=False) except Exception as e: jcs.output("Failed to connect to the JET app %s" %e.message) return lw = lwaftr_stats() """ try: global read_list, timeout while read_list: ready = select.select(read_list, [], [], timeout)[0] if not ready: lw.monitor(server) else: exit(0) except KeyboardInterrupt: pass """ while True: lw.monitor(server,argv) time.sleep(2)
def main(): # jcs.output => Output the message to CLI jcs.output('testing jcs output ') # jcs.get_input => Prompt user for input, echoed back to user user_input = jcs.get_input(' ') # Dump the user entered output jcs.output(user_input) # jcs.get_secret => Prompt user for input, not echoed back to user user_input = jcs.get_secret(' ') # Dump the user entered output jcs.output(user_input) # Run the script with 'cli> op extensions.py detail ' to view progress message jcs.progress("Progress message from python op-script") # Syslog the message jcs.syslog("pfe.alert", "Sample syslog message from python op-script") jcs.syslog("161", "Sample syslog message from python op-script") # Getting hostname of box (Please note DNS needs to be configured) hostname = jcs.hostname("bng-ui-vm-05") print hostname # SYSCTL information osrelease = jcs.sysctl("kern.osrelease", "s") slotid = jcs.sysctl("hw.re.slotid", "i") print osrelease print slotid # This is from jcs.printf(...) jcs.printf("%s", "JUNOS") # Send information to configured trace file using jcs.trace(...) jcs.trace("teting jcs trace") # Emit warning message to console jcs.emit_warning("Warning message from Python op script") # Dampening script execution based on return value dampen_value = jcs.dampen('TEST', 3, 10) print dampen_value # Emit error message to console jcs.emit_error("Error message from Python op script")
def monitor(self, server, argv): try: newstats = server.lwaftr() except Exception as e: jcs.output("%s" %e.message) exit(0) # newstats contains all the latest stats instance_id = "" for i in range(0,len(argv)): if argv[i] == "id": instance_id = argv[i+1] break newtime = time.time() root = ET.fromstring(newstats) found = 0 for instance in root: if instance_id != "" and instance.findall("./id")[0].text != argv[2]: pass else: id = instance.findall("./id")[0].text if id in self.ids: pass else: self.ids.append(id) self.lwaftr[id] = [] self.lwaftr_old[id] = [] self.lwaftr_ps[id]=[] self.lwaftr_ifInDiscards[id]= "0" self.lwaftr_ifInDiscards_old[id] = "0" self.lwaftr_ifInDiscards_ps[id] = 0.0 self.breaths[id] = "0" self.breaths_old[id] = "0" self.breaths_ps[id] = 0.0 for i in range(0,len(self.index_dict)): self.lwaftr[id].append("0") self.lwaftr_old[id].append("0") self.lwaftr_ps[id].append(0.0) found = 1 for child_instance in instance: if child_instance.tag == "apps": for app_child in child_instance: if app_child.tag == "lwaftr": for lwaftr_child in app_child: tag = lwaftr_child.tag if self.index_dict.has_key(tag): # set the corresponding counters if lwaftr_child.text.strip() == "": # TODO should we just leave it like that? # as of now i am just updating the old vales to the new one and set ps to 0 self.lwaftr_old[id][self.index_dict[tag]] = 0 self.lwaftr[id][self.index_dict[tag]] = 0 self.lwaftr_ps[id][self.index_dict[tag]] = 0.0 else: self.lwaftr_old[id][self.index_dict[tag]] = self.lwaftr[id][self.index_dict[tag]] self.lwaftr[id][self.index_dict[tag]] = lwaftr_child.text.strip() if self.previous_poll != 0: delta = float(newtime - self.previous_poll) nwval = float(int(self.lwaftr[id][self.index_dict[tag]])-int(self.lwaftr_old[id][self.index_dict[tag]])) nwval = round(nwval,2) self.lwaftr_ps[id][self.index_dict[tag]] = nwval/delta else: if tag == "ingress-packet-drops": if lwaftr_child.text.strip() == "": self.lwaftr_ifInDiscards_old[id] = self.lwaftr_ifInDiscards[id] self.lwaftr_ifInDiscards_ps[id] = 0.0 else: self.lwaftr_ifInDiscards_old[id] = self.lwaftr_ifInDiscards[id] self.lwaftr_ifInDiscards[id] = lwaftr_child.text.strip() if self.previous_poll != 0: delta = float(newtime - self.previous_poll) nwval = float(int(self.lwaftr_ifInDiscards[id])-int(self.lwaftr_ifInDiscards_old[id])) nwval = round(nwval,2) self.lwaftr_ifInDiscards_ps[id] = nwval/delta if child_instance.tag == "engine": for engine_child in child_instance: if engine_child.tag == "breaths": if engine_child.text.strip() == "": self.breaths_old[id] = self.breaths[id] self.breaths_ps[id] = 0.0 else: self.breaths_old[id] = self.breaths[id] self.breaths[id] = engine_child.text.strip() if self.previous_poll != 0: delta = float(newtime - self.previous_poll) nwval = float(int(self.breaths[id])-int(self.breaths_old[id])) nwval = round(nwval,2) self.breaths_ps[id] = nwval/delta self.display_monitor(id) if found == 0: jcs.output("Invalid instance") exit(0) # update the self.previous_poll at the end self.previous_poll = newtime
import os import subprocess sys.path.append("/var/db/scripts/action") sys.path.append("/var/db/scripts/import") args = {'list': None} for arg in args.keys(): if arg in sys.argv: index = sys.argv.index(arg) args[arg] = sys.argv[index+1] if args["list"] == "ram": out = subprocess.Popen(['free', '-h'],stdout=subprocess.PIPE, stderr=subprocess.STDOUT) jcs.output(out.communicate()[0]) if args["list"] == "hdd": out = subprocess.Popen(['df', '-h'],stdout=subprocess.PIPE, stderr=subprocess.STDOUT) jcs.output(out.communicate()[0]) if args["list"] == "vmstats": out = subprocess.Popen(['vmstat'],stdout=subprocess.PIPE, stderr=subprocess.STDOUT) if args["list"] == "cpu": out = subprocess.Popen(['iostat', '-c'],stdout=subprocess.PIPE, stderr=subprocess.STDOUT) if args["list"] == "meminfo" out = subprocess.Popen(['more', '/proc/meminfo'],stdout=subprocess.PIPE, stderr=subprocess.STDOUT) if args["list"] == "softirq"