Monitoring shared storage path

No monitoring is not operation and maintenance, although I these posts may be for Google-level operation and maintenance disdain (although I also play virtualization, public private cloud, cluster), but these are very down to earth real case, and do not spend a money for the novice to change to change can be used directly for the master used in large-scale projects have the finishing touch, the icing on the cake effect.

Technology is to play more, play Chuming Tang.

Similarly, a brief background of the demand:

Now any one company, if it is not on shared storage (either NAS or EMC, whether distributed or disk arrays, whether ISCSI, or SAN), I think it is still in the primitive society.

The most intuitive user logon is a network path \\ IP address or map a network drive to a local, when used too common, the problem came.

Just arrived a few days, the boss design department to trouble, saying they design documents stored in shared work path of the file is lost or not normal (file name can be seen, but the size is 0), my first reaction NAS is a problem, but experience tells me QNAP NAS enterprise is not so do not fly,

In the design department learned that work is directly open 2D or 3D design software design files directly from a network drive, or a new design software to save files directly to a network disk path;

In my opinion the purpose of doing so is to be lazy, eliminating the need for a hard disk stored locally and then uploading and downloading to the shared storage process, not impossible, but do not recommend, and why?

1, directly to the open network disk files across the network, local area network stability, better said, if the jitter it? (Occasionally jitter, the devil knows it? Let alone find a cause), half of the document do not waste time to save it.

2, not silly money for general corporate or reluctant on EMC SAN (high reliability, high availability, high speed), put forward a general entry-level NAS with a Fast switch (even if gigabit device, in case of bad Internet line construction, where up to automatically slow down to less than Gigabit Fast), network disk file upload and download speeds of 10M also, the speed, and turn on the local hard disk to store documents is simply different, in order to save a bit of trouble, open a few dozen M the 3D drawing to wait half a minute, silly not stupid, fix, no direct response to the crash you see.

3, on the local hard disk editing the job file and then uploading and downloading to a network disk process is explicit, failure success, clear, and directly through the software work directly to the network disk pull and save the document is implicit, working software background You do, do not do succeed, no way of knowing if a software bug, and obviously the software save the document, but the display is not normal on the NAS.

I wealth of business experience in the operation and maintenance point of view I have summarized above, but the time to explain the design department boss to listen to, but not recognized, several times down, I am angry, engage in technology as a need to use technology to arm themselves, to prove his point.

So, this tutorial will give it to those who have not understood this to be all kinds of harm to colleagues:

First of all, I think I need to share design department of the disk inside the file directory and size, modification time create a snapshot of a virtual machine like beat, like, take a snapshot of the timing for my query retroactive.

As play linux this is very easy to handle, the basic idea is the need to monitor the network disk to mount linux inside, then go to implementation of a regular tree command to get use crontab, he made it simple or go several steps:

1, CentOS mount NAS shared disk:

Pictures .png

Preferably added to /etc/rc.local startup file inside, so open automatically mount;

2, edit the script file, timed to the size of the log output directory:

tree --charset UTF-8  /root/design/design/ -s -D  -h -N >/root/design_monitor/design.dir
tree --charset UTF-8  /root/design/photo/  -s -D  -h -N >/root/design_monitor/photo.dir

Run the shell script that will get shared directory tree two main surveillance, the following figure to take the design of this catalog, for example, open design.dir script file output:

Pictures .png

There is not enough light in this description of the problem, I need to know the staff has done every day in the network disk, hard work pays off, I found a weapon: Directory Monitor (free, advanced features fee), but the free basic version has been good enough, the internet has cracked, but I do not need:

Pictures .png

After installing the software on the window server, the server does not shut down all the year round, it is equivalent to 7X24-hour monitoring of the directory, this stuff is very powerful, very professional, of course, very white (for beginners), easy to use:

Pictures .png

Defined the monitoring parameters as well as log output path position (I directly put in the shared disk monitoring, easy access linux):

Pictures .png

Get a directory operation log something like this:

Pictures .png

我这人很懒,这么闹心的一件事,我不想每天都惦记着,我得让它自动化,每天发个邮件把上面所有的东东作为附件到我和那位设计部老大的邮箱里,啥时候出现问题,自己去追溯,不要说我没有证据,想仿照DiretoryMonitor一样不用打开附件就直接能看到漂亮的操作日志,就做一下html格式化。

那么,在linux下面又弄个python程序去做这件事,直接上干货:

# !/usr/bin/python3

import smtplib
import os , time
from email.mime.text import MIMEText
from email.header import Header
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart

today = time.localtime(time.time())
today = time.strftime('%Y%m%d', today)

def lastestfile(path):
        filenamelist = os.listdir(path)
        tm = os.stat(path + filenamelist[0])
        name = filenamelist[0]
        for item in filenamelist:
                temp = os.stat(path + item)
                if tm.st_ctime < temp.st_ctime:
                        tm = temp
                        name = item

        return name

design_log_path = '/root/design/Log/design_log/'
design_log_name =  lastestfile(design_log_path)
design_log_path = design_log_path + design_log_name

photo_log_path = '/root/design/Log/photo_log/'
photo_log_name =  lastestfile(photo_log_path)
photo_log_path = photo_log_path + photo_log_name

def htmlContent(path):

    log_text = open(path, 'r',encoding='utf-8')
    lines = log_text.readlines()
    head = "<table border='0' align='center'  style='font-size:14px;word-break: keep-all'><tr bgcolor='3F48CC'><th><font color='ffffff'>操 作</font></th><th><font color='ffffff'>执 行 时 间</font></th><th><font color='ffffff'>文 件 路 径</font></th></tr>"

    tmp = ""

    for line in lines:
        if line.find("新增")>=0:
            t = line.index(' ')
            tmp = tmp + "<tr bgcolor='9DF9B8'><td align='center'   width = 45><font color='198A3A'><b>" + line[0:t] + "</b></font></td><td width = 150><font color='000000'>" + (line[t + 1:].split(': '))[0] + "</font></td><td><font color='000000'>" + (line[t + 1:].split(': '))[1] + "</font></td></tr>"

        elif line.find("修改")>=0:
            t = line.index(' ')
            tmp = tmp + "<tr bgcolor='BDE8FA'><td align='center'   style= 'word-break: keep-all'><font color='3F48CC'><b>" + line[0:t] + "</b></font></td><td><font color='000000'>" + (line[t + 1:].split(': '))[0] + "</font></td><td><font color='000000'>" + (line[t + 1:].split(': '))[1] + "</font></td></tr>"

        elif line.find("重命名")>=0:
            t = line.index(' ')
            tmp = tmp +  "<tr bgcolor='FCFAD1'><td align='center'  style= 'word-break: keep-all'><font color='E37C35'><b>" + line[0:t] + "</b></font></td><td><font color='000000'>" + (line[t + 1:].split(': '))[0] + "</font></td><td><font color='000000'>" + (line[t + 1:].split(': '))[1] + "</font></td></tr>"
         elif line.find("删除")>=0:
            t = line.index(' ')
            tmp = tmp + "<tr bgcolor='FCDEDF'><td align='center'  style= 'word-break: keep-all'><font color='ED1C24'><b>" + line[0:t] + "</b></font></td><td><font color='000000'>" + (line[t + 1:].split(': '))[0] + "</font></td><td><font color='000000'>" + (line[t + 1:].split(': '))[1] + "</font></td></tr>"

        else:

            pass


    htmlContent=head + tmp + "</table>"
    return htmlContent




sent_content = "<table border='0' align='center' nowrap><caption>Directory Monitor---【design】</caption><tr><td>" + htmlContent(design_log_path) + "</td></tr></table><br><table border='0' align='center' ><caption>Directory Monitor---【photo】</caption><tr><td>" + htmlContent(photo_log_path) + "</td></tr></table>"

_user = "邮箱账号"
_pwd = "密码"
_to = "设计部和我的邮箱群组"


msg = MIMEMultipart()
msg["Subject"] = "Design-Dep NAS File Snapshot"
msg["From"] = _user
msg["To"] = _to


part = MIMEText(sent_content, 'html', 'utf-8')
msg.attach(part)


part = MIMEApplication(open('/root/py_script/design_monitor/design.dir', 'rb').read())
part.add_header('Content-Disposition', 'attachment', filename="design_" + today + ".dir")
msg.attach(part)


part = MIMEApplication(open('/root/py_script/design_monitor/photo.dir', 'rb').read())
part.add_header('Content-Disposition', 'attachment', filename="photo_" + today + ".dir")
msg.attach(part)


part = MIMEApplication(open(design_log_path, 'rb').read())
part.add_header('Content-Disposition', 'attachment', filename=design_log_name)
msg.attach(part)

part = MIMEApplication(open(photo_log_path, 'rb').read())
part.add_header('Content-Disposition', 'attachment', filename=photo_log_name)
msg.attach(part)

s = smtplib.SMTP ( "Mailbox Server Address", timeout = 30) 
s.login (_user, _pwd) 
s.sendmail (_user, _TO, msg.as_string ()) 
S.CLOSE ()

The last show renderings:

Pictures .png

What is good, and finally, the next time the boss again complained that the design of the design documentation is abnormal, and I will look through the daily operation log This document is created when (their own documents, without looking at the log he probably knows when done),

By operating the log to find new day, then the file tree command seized the day's poor output directory tree, found that the day is to create unusual, indicating that the software is not designed properly to save the network disk, do not blame anyone and equipment;

To blame blame him too much faith in its design software design and preservation of important documents on a network drive do not go on the spot confirmed that there is no saved successfully.

In this way, the Department of Design nagging from their mouths shut, to clean my ears, sometimes with no explanation, they want proof.

Pictures .png

Guess you like

Origin blog.51cto.com/kingda/2425831