|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "regexp" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + "sync" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + OK = 0 |
| 16 | + NONOK = 1 |
| 17 | + UNKNOWN = 2 |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + procPath string |
| 22 | + printCount bool |
| 23 | + percentThreshold int |
| 24 | +) |
| 25 | + |
| 26 | +func init() { |
| 27 | + flag.StringVar(&procPath, "p", "/proc", "actual path of /proc") |
| 28 | + flag.BoolVar(&printCount,"c", false, "print numbers of used df and max") |
| 29 | + flag.IntVar(&percentThreshold, "t", 80, "Warning threshold of percentage of fd usage to max limitation") |
| 30 | +} |
| 31 | + |
| 32 | +func main() { |
| 33 | + flag.Parse() |
| 34 | + |
| 35 | + if percentThreshold >= 100 || percentThreshold <=0 { |
| 36 | + fmt.Fprintf(os.Stderr, "value of -t must between 0 and 100") |
| 37 | + os.Exit(UNKNOWN) |
| 38 | + } |
| 39 | + |
| 40 | + maxPath := procPath + "/sys/fs/file-max" |
| 41 | + f, err := os.Open(maxPath) |
| 42 | + if err != nil { |
| 43 | + fmt.Fprintf(os.Stderr, "%v", err) |
| 44 | + os.Exit(UNKNOWN) |
| 45 | + } |
| 46 | + defer f.Close() |
| 47 | + maxBytes, err := ioutil.ReadAll(f) |
| 48 | + if err != nil { |
| 49 | + fmt.Fprintf(os.Stderr, "%v", err) |
| 50 | + os.Exit(UNKNOWN) |
| 51 | + } |
| 52 | + fdMax, err := strconv.Atoi(strings.TrimSpace(string(maxBytes))) |
| 53 | + if err != nil { |
| 54 | + fmt.Fprintf(os.Stderr, "%v", err) |
| 55 | + os.Exit(UNKNOWN) |
| 56 | + } |
| 57 | + |
| 58 | + files, err := ioutil.ReadDir(procPath) |
| 59 | + if err != nil { |
| 60 | + fmt.Fprintf(os.Stderr, "%v", err) |
| 61 | + os.Exit(UNKNOWN) |
| 62 | + } |
| 63 | + |
| 64 | + ch := make(chan int) |
| 65 | + wg := sync.WaitGroup{} |
| 66 | + re := regexp.MustCompile(`[0-9][0-9]*`) |
| 67 | + for _, f := range files { |
| 68 | + if f.IsDir() { |
| 69 | + if re.MatchString(f.Name()) { |
| 70 | + wg.Add(1) |
| 71 | + go countFD(procPath+"/"+f.Name()+"/fd", &wg, ch) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + go func() { |
| 77 | + wg.Wait() |
| 78 | + close(ch) |
| 79 | + }() |
| 80 | + |
| 81 | + fdTotal := 0 |
| 82 | + for { |
| 83 | + c, ok := <-ch |
| 84 | + if !ok { |
| 85 | + break |
| 86 | + } |
| 87 | + fdTotal += c |
| 88 | + } |
| 89 | + |
| 90 | + if printCount { |
| 91 | + fmt.Fprintf(os.Stdout, "current fd usage is %d and limitaion is %d\n", fdTotal, fdMax) |
| 92 | + } |
| 93 | + if fdTotal > fdMax / 100 * percentThreshold { |
| 94 | + fmt.Fprintf(os.Stdout, "current fd usage is %d and is over %d of limition %d, \n", |
| 95 | + fdTotal, percentThreshold, fdMax) |
| 96 | + os.Exit(NONOK) |
| 97 | + } else { |
| 98 | + fmt.Fprintf(os.Stdout, "node has no fd pressure\n") |
| 99 | + os.Exit(OK) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func countFD(path string, wg *sync.WaitGroup, fNum chan<- int) { |
| 104 | + defer wg.Done() |
| 105 | + files, err := ioutil.ReadDir(path) |
| 106 | + if err != nil { |
| 107 | + fmt.Fprintf(os.Stderr, "%v", err) |
| 108 | + return |
| 109 | + } |
| 110 | + fNum <- len(files) |
| 111 | +} |
0 commit comments