Python monitors linux memory and writes it to mongodb (recommended) and pythonmongodb

Source: Internet
Author: User

Python monitors linux memory and writes it to mongodb (recommended) and pythonmongodb

(You need to install psutil to obtain server resources and pymongo driver) # pip install psutil

#pip install pymongo#vim

The file content is as follows:

#! /Usr/bin/env python #-*-coding: UTF-8-*-import psutilimport socketimport timefrom pymongo import Export clientmongodbip = '100. 168.200.112 'mongodbport = 27017 hostname = socket. gethostbyname (socket. gethostname () # obtain the local IP address def getCurrentTime (): return time. strftime ('% Y-% m-% d % H: % M: % s', time. localtime (time. time () memoryInfo = psutil. virtual_memory () conn = MongoClient (mongodbIp, mongodbPort) db = conn. servermonitordbset = db. memorydbset. insert ({'time': getCurrentTime (), 'Total': memoryInfo. total, 'available': memoryInfo. available, 'percent ': memoryInfo. percent, 'used': memoryInfo. used, 'free': memoryInfo. free, 'active': memoryInfo. active, 'inactive': memoryInfo. inactive, 'buffers': memoryInfo. buffers, 'cached': memoryInfo. cached })

You can directly modify the file.

#chmod +x

Use crontab to regularly execute monitoring programs

#vim vim /etc/crontab

Add the following content (executed once per minute)

*/1 ***** root/usr/local/ # service crond reload // reload configuration # service crond restart // restart the service


The above section describes how to monitor linux memory and write data into mongodb in python. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.