-
Notifications
You must be signed in to change notification settings - Fork 142
modules/dnsmasq_dhcp: respect conf-file/conf-dir #239
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| .idea | ||
| bin/ | ||
| mocks/springboot2/.gradle/ | ||
| /godplugin |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,28 +6,42 @@ import ( | |
| "io/ioutil" | ||
| "net" | ||
| "os" | ||
| "path" | ||
| "path/filepath" | ||
| "regexp" | ||
| "strings" | ||
| ) | ||
|
|
||
| // configDir represents conf-dir directive | ||
| // | ||
| // #conf-dir=/etc/dnsmasq.d | ||
| // | ||
| // # Include all the files in a directory except those ending in .bak | ||
| // #conf-dir=/etc/dnsmasq.d,.bak | ||
| // | ||
| // # Include all files in a directory which end in .conf | ||
| // #conf-dir=/etc/dnsmasq.d/,*.conf | ||
| type configDir struct { | ||
| path string | ||
| includeSuffix []string | ||
| excludeSuffix []string | ||
| } | ||
|
|
||
| func (d *DnsmasqDHCP) findDHCPRanges() ([]string, error) { | ||
| cs, err := findConfigurations(d.ConfDir) | ||
| if err != nil { | ||
| d.Warningf("error during configuration dir scanning : %v", err) | ||
| } | ||
| configs := d.findConfigs(d.ConfPath) | ||
|
|
||
| configs = append([]string{d.ConfPath}, configs...) | ||
|
|
||
| configs = unique(configs) | ||
|
|
||
| configs := []string{d.ConfPath} | ||
| configs = append(configs, cs...) | ||
| d.Infof("configuration files to read : %v", configs) | ||
| d.Infof("configuration files to read: %v", configs) | ||
|
|
||
| seen := make(map[string]bool) | ||
| var ranges []string | ||
|
|
||
| for _, config := range configs { | ||
| d.Debugf("reading %s", config) | ||
| rs, err := findDHCPRanges(config) | ||
| if err != nil { | ||
| if err != nil && !os.IsNotExist(err) { | ||
| return nil, err | ||
| } | ||
|
|
||
|
|
@@ -46,21 +60,96 @@ func (d *DnsmasqDHCP) findDHCPRanges() ([]string, error) { | |
| return ranges, nil | ||
| } | ||
|
|
||
| func findConfigurations(confDir string) ([]string, error) { | ||
| fis, err := ioutil.ReadDir(confDir) | ||
| // findConfigs recursively finds and reads configuration files respecting | ||
| // conf-file and conf-dir directives. | ||
| // findConfigs is tolerant to IO errors and finds as maximum config | ||
| // files as possible, therefore if an error occurrs during scanning process, | ||
| // it will be just logged with warning severity | ||
| func (d *DnsmasqDHCP) findConfigs(confPath string) []string { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
having it this way we need to ^^ looks like a workaround or smth
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't touch that logic though https://github.com/netdata/go.d.plugin/pull/239/files#diff-daece282ff93570d72a99d7ffa78dfd5L20
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i know, i know. it was which is ok (but not good ofc), if finds configs in dir. we can fix it later. One more thing i noticed, your implementation ignores
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, maybe we need to get rid of All we need is the entry config
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On debian it reads from
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, it seems but, as i said it doesnt work, so we can ignore it.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i finally read it looks like we need both |
||
| config, err := os.Open(confPath) | ||
| if err != nil { | ||
| return nil, err | ||
| d.Warningf("error during configuration file %q reading: %v", confPath, err) | ||
| return nil | ||
| } | ||
|
|
||
| var configs []string | ||
| for _, fi := range fis { | ||
| if !fi.Mode().IsRegular() || !strings.HasSuffix(fi.Name(), ".conf") { | ||
| defer config.Close() | ||
|
|
||
| var ( | ||
| includeFiles []string | ||
| includeDirs []configDir | ||
| ) | ||
|
|
||
| scanner := bufio.NewScanner(config) | ||
| for scanner.Scan() { | ||
| line := scanner.Text() | ||
|
|
||
| if path, ok := getConfValue(line, "conf-file"); ok { | ||
| includeFiles = append(includeFiles, path) | ||
| continue | ||
| } | ||
|
|
||
| if path, ok := getConfValue(line, "conf-dir"); ok { | ||
| args := strings.Split(path, ",") | ||
|
|
||
| dir := configDir{ | ||
| path: args[0], | ||
| } | ||
|
|
||
| for _, arg := range args[1:] { | ||
| arg := strings.TrimSpace(arg) | ||
| // dnsmasq treats suffixes with asterisk as "to include" and without | ||
| // asterisk as "to exclude" | ||
| if strings.HasPrefix(arg, "*") { | ||
| dir.includeSuffix = append(dir.includeSuffix, arg[1:]) | ||
| } else { | ||
| dir.excludeSuffix = append(dir.excludeSuffix, arg) | ||
| } | ||
| } | ||
|
|
||
| includeDirs = append(includeDirs, dir) | ||
|
|
||
| continue | ||
| } | ||
| configs = append(configs, path.Join(confDir, fi.Name())) | ||
| } | ||
|
|
||
| return configs, nil | ||
| for _, dir := range includeDirs { | ||
| dirFiles, err := dir.findConfigs() | ||
| if err != nil { | ||
| d.Warningf("error during configuration dir %q scanning: %v", dir.path, err) | ||
| } | ||
|
|
||
| includeFiles = append(includeFiles, dirFiles...) | ||
| } | ||
|
|
||
| for _, file := range includeFiles { | ||
| files := d.findConfigs(file) | ||
kovetskiy marked this conversation as resolved.
Show resolved
Hide resolved
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think we need to avoid infinite recursion here (it is not infinite, i know) that is what i get, fatal error doesnt look good |
||
|
|
||
| includeFiles = append(includeFiles, files...) | ||
| } | ||
|
|
||
| return includeFiles | ||
| } | ||
|
|
||
| func getConfValue(line, prefix string) (value string, ok bool) { | ||
| if !strings.HasPrefix(line, prefix) { | ||
| return "", false | ||
| } | ||
|
|
||
| value = strings.TrimPrefix(line, prefix) | ||
|
|
||
| value = strings.TrimSpace(value) | ||
|
|
||
| if !strings.HasPrefix(value, "=") { | ||
| // got some unexpected line, has prefix like conf-file but there is no | ||
| // assign sign | ||
| return "", false | ||
| } | ||
|
|
||
| value = strings.TrimPrefix(value, "=") | ||
|
|
||
| value = strings.TrimSpace(value) | ||
|
|
||
| return value, true | ||
| } | ||
|
|
||
| func findDHCPRanges(filePath string) ([]string, error) { | ||
|
|
@@ -128,3 +217,95 @@ func parseDHCPRangeLine(s string) (r string) { | |
|
|
||
| return fmt.Sprintf("%s-%s", start, end) | ||
| } | ||
|
|
||
| func (dir configDir) findConfigs() ([]string, error) { | ||
| fis, err := ioutil.ReadDir(dir.path) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var configs []string | ||
|
|
||
| for _, fi := range fis { | ||
| if !fi.Mode().IsRegular() { | ||
| continue | ||
| } | ||
|
|
||
| name := fi.Name() | ||
| if !dir.isValidFileName(name) { | ||
| continue | ||
| } | ||
ilyam8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if !dir.matchFileName(name) { | ||
| continue | ||
| } | ||
|
|
||
| configs = append(configs, filepath.Join(dir.path, fi.Name())) | ||
| } | ||
|
|
||
| return configs, nil | ||
| } | ||
|
|
||
| func (dir configDir) isValidFileName(name string) bool { | ||
| // We copy the dnsmasq's logic | ||
| // | ||
| // /* ignore emacs backups and dotfiles */ | ||
| // if (len == 0 || | ||
| // ent->d_name[len - 1] == '~' || | ||
| // (ent->d_name[0] == '#' && ent->d_name[len - 1] == '#') || | ||
| // ent->d_name[0] == '.') | ||
| // continue; | ||
|
|
||
| if strings.HasSuffix(name, "~") || | ||
| (strings.HasPrefix(name, "#") && strings.HasSuffix(name, "#")) || | ||
| strings.HasPrefix(name, ".") { | ||
| return false | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| func (dir configDir) matchFileName(name string) bool { | ||
| if len(dir.includeSuffix) > 0 { | ||
| including := false | ||
| for _, suffix := range dir.includeSuffix { | ||
| if strings.HasSuffix(name, suffix) { | ||
| including = true | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if !including { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| for _, suffix := range dir.excludeSuffix { | ||
| if strings.HasSuffix(name, suffix) { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| func unique(slice []string) []string { | ||
| result := []string{} | ||
|
|
||
| for _, item := range slice { | ||
| if !contains(result, item) { | ||
| result = append(result, item) | ||
| } | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| func contains(slice []string, target string) bool { | ||
| for _, item := range slice { | ||
| if item == target { | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| dhcp-range=192.168.2.0,192.168.2.9,12h | ||
| conf-file=testdata/dnsmasq.more.conf |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| dhcp-range= 172.168.0.0,172.168.0.9,12h | ||
| dhcp-range=tag:green,192.168.1.0,192.168.1.9,12h |
Uh oh!
There was an error while loading. Please reload this page.