简体   繁体   中英

Ping multiple ips and write to JSON file python

I am pinging multiple ips in the LAN to check if it is alive or not. the code will be run every minute based on schedule.For pinging multiple ips, i used multiprocessing. It is done great with the help of multiprocessing. Simultaneously, i want to write the ping results to the json file after pinging. But When writing to the JSON file, it is writing only the output of the last ip.I want all the three. Is there any way to do this

Here's the sample Code:

import json
from multiprocessing import Pool
import subprocess
from datetime import datetime
timestamp = datetime.now().strftime("%B %d %Y, %H:%M:%S")
hosts =  ["192.168.1.47","192.168.1.42"]
count = 1
wait_sec = 1
n = len(hosts)
def main(hosts):
    p = Pool(processes= n)
    result = p.map(beat, hosts)
def beat(hosts):
    #Name for the log file
    name = 'icmp.json'
    ip4write(hosts, name)
def ip4write(hosts, name):
    global ip4a
    ip4a = hosts
    ipve4(hosts, name)
    write(hosts, name)
def ipve4(hosts, name):
    global u
    status, result = subprocess.getstatusoutput("ping -c1 -w2 " + str(ip4a))
    if status == 0:
        print(str(ip4a) + " UP")
        u = " UP"
def write(hosts, name):
    text_file = open(name, "a+")
    with open(name) as json_file:
      try:
          data = json.load(json_file)
      except:
          data = {}
      with open(name, 'w') as outfile:
        data[timestamp] = {
          'monitor.ip':str(hosts),
          'monitor.status': u
        }
        print(data)
        json.dump(data, outfile)
        print('Data written')
    text_file.close()
main(hosts)

Output in JSON file:

{"February 15 2019, 16:38:12": {"monitor.status": " UP", "monitor.ip": "192.168.1.42"}}

My required Output:

{"February 15 2019, 16:38:12": {"monitor.ip": "192.168.1.47", "monitor.status": " UP"}, "February 15 2019, 16:38:12": {"monitor.ip": "192.168.1.42", "monitor.status": " UP"}}

To keep adding contents to an existing file without overwriting existing content, you should open in the "append" mode. In your code, you are opening in "write" mode. Which will open the file for writing but will overwrite existing contents.

Specifically, this line in your code:

with open(name, 'w') as outfile:

You should change the open mode from write ( 'w' ) to append ( 'a' ).

with open(name, 'a') as outfile:

Let me know if this solves your problem.

Below is a compact version of the code:

import os
from multiprocessing import Pool
import json
import datetime
import time

hosts = ["192.168.1.47", "8.8.8.8"]
MAX_NUMBER_OF_STATUS_CHECKS = 2
FILE_NAME = 'hosts_stats.json'


#
# counter and sleep were added in order to simulate scheduler activity  
#

def ping(host):
    status = os.system('ping  -o -c 3 {}'.format(host))
    return datetime.datetime.now().strftime("%B %d %Y, %H:%M:%S"), {"monitor.ip": host,
                                                                "monitor.status": 'UP' if status == 0 else 'DOWN'}


if __name__ == "__main__":
    p = Pool(processes=len(hosts))
    counter = 0
    if not os.path.exists(FILE_NAME):
        with open(FILE_NAME, 'w') as f:
            f.write('{}')
    while counter < MAX_NUMBER_OF_STATUS_CHECKS:
        result = p.map(ping, hosts)
        with open(FILE_NAME, 'rb+') as f:
            f.seek(-1, os.SEEK_END)
            f.truncate()
            for entry in result:
                _entry = '"{}":{},\n'.format(entry[0], json.dumps(entry[1]))
                f.writelines(_entry)
             f.write('}')
        counter += 1
        time.sleep(2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM