Zabbix check HDD s.m.a.r.t status [go language implementation]

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Because the server is more, used to like to write programs in Perl, and later found that the deployment of the Perl environment is also installed, it is too troublesome, so instead of go programming, go to compile the post, copy to the server can run, and really busty convenient.

have already used go to write a MySQL master-slave synchronization, check whether the error from the server program, if the synchronization error can be in the Zabbix warning, the MySQL password written to the program, and then compiled, so much more secure than the scripting language, and do not add a user.

The reason is that, there are a lot of servers, a large part of the ordinary hard disk, the ordinary hard disk is prone to problems, because there is no way on the server to do badblock check (too much consumption of IO resources), but check the S.M.A.R.T state can anticipate the hard disk failure, or a bit of use, better than no, right.

The program is based on Linux, window changes can also be used, mainly to check the/DEV/SDA...B...C...D loop check the system has how many hard disks, and then check the status, if the program returned 1 normal, the program returned 0 indicates that there is a problem with the hard disk.

package mainimport ("bytes" "Flag" "FMT" "Log" "OS" "Os/exec" "strings") Func pathexists (path String) (bool, error) {_, Err: = OS. Stat (path) if Err = = Nil {return true, Nil}if OS. Isnotexist (Err) {return false, Nil}return false, Err}func smartctl (dev string, debug int) int {path, err: = Exec. Lookpath ("Smartctl") if err! = Nil {log. Fatal (err)}cmd: = Exec.command (Path, "-H", Dev) var out bytes. Buffercmd.stdout = &outerr = cmd. Run () if err! = Nil {log. Fatal (Err)}//fmt. Printf ("%s\n", out. String ()) if strings. Contains (out). String (), "OK") | | Strings. Contains (out). String (), "PASSED") {if debug = = 1 {fmt. Printf ("%s test ok\n", Dev)}return 1}return 0}func Main () {debug: = flag. Int ("Debug", 0, "to debug-debug 1") flag. Parse () for _, Drive: = Range "ABCD" {//dev:=fmt. Sprintf ("/dev/sd%s", drive) Dev: = "/dev/sd" + string (drive)//fmt. Println (Dev) If ok, _: = Pathexists (dev); OK {mystat: = Smartctl (Dev, *debug) if Mystat ==0 {fmt. Print (0) OS. Exit ( -1)}}}fmt. Print (1)} 

 

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: info-contact@alibabacloud.com 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.