Skip to content

Commit

Permalink
Add support for subdirectories scan, Add scan_subdir and logging_leve…
Browse files Browse the repository at this point in the history
…l properties
  • Loading branch information
elghali committed Oct 14, 2022
1 parent e183d93 commit 0cfe364
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 28 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ Usage
```
git clone https://github.com/elghali/nfs_exporter_py.git
cd nfs_exporter_py
python3 script-exporter-py [dir_path]
python3 script-exporter-py
[Try it](https://loclhost:9469)
```

## Docker

`docker run -it -v "[dir_path]:[dir_path]" -p 9469:9469 -e dir_path="[dir_path]" -d elghali/nfs_exporter_py:1.0`
`docker run -it -v "[dir_path]:[dir_path]" -p 9469:9469 -e dir_path="[dir_path]" -d elghali/nfs_exporter_py:2.0`

## K8s
To try the exporter on K8s we can use the below sample deployment or visit [examples](https://github.com/elghali/nfs_exporter_py/examples) for more examples.
Expand All @@ -32,9 +32,11 @@ metadata:
app.kubernetes.io/name: nfs-exporter-py
data:
config.yaml: |
dir_path: /home/elghali,/home/elghali/myFiles
dir_path: [/home/elghali,/home/elghali/myFiles]
scan_subdir: [False,True]
port: 9469
metric_freq: 60
logging_level: DEBUG
---
apiVersion: apps/v1
kind: Deployment
Expand All @@ -55,7 +57,7 @@ spec:
spec:
containers:
- name: nfs-exporter-py
image: elghali/nfs_exporter_py:1.4
image: elghali/nfs_exporter_py:2.0
ports:
- name: http
containerPort: 9469
Expand Down Expand Up @@ -101,6 +103,8 @@ spec:

| Option | Default | Description
| ----------- | ----------- | ----------- |
| dir_path | /opt | NFS path(s) to montior
| dir_path | ['/opt'] | NFS path(s) to montior
| scan_subdir | [False] | Include subdirectories in count (True) or not (False)
| port | 9469 | Exposed port |
| metric_freq | 10 | Metrics scraping frequency |
| metric_freq | 10 | Metrics scraping frequency |
| logging_level | DEBUG | Logging Level
6 changes: 4 additions & 2 deletions examples/config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
dir_path: /home/elghali,/home/elghali/myFiles
dir_path: [/home/elghali,/home/elghali/myFiles]
scan_subdir: [False,True]
port: 9469
metric_freq: 60
metric_freq: 60
logging_level: DEBUG
62 changes: 42 additions & 20 deletions nfs_exporter_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,65 @@
import os, sys, logging
import yaml

#Basic Logging
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
logger = logging.getLogger()

#Default Values
dir_path = ['/opt']
scan_subdir = [False]
metric_freq = 10
port = 9469
logging_level= 'DEBUG'

logging.basicConfig(stream=sys.stdout, level=logging_level.upper())
logger = logging.getLogger()

def loadConfig():
global dir_path, scan_subdir, port, metric_freq, logging_level, logger
if os.path.exists('./config.yaml'):
logger.debug('Config file found')
with open('./config.yaml', 'r') as config_file:
try:
config = yaml.safe_load(config_file)
#Required configs
if all(key in config for key in ('dir_path','scan_subdir','port','metric_freq','logging_level')):
dir_path = config['dir_path']
scan_subdir = config['scan_subdir']
port = int(config['port'])
metric_freq = int(config['metric_freq'])
logging_level = config['logging_level']
logger.setLevel(logging_level.upper())
else:
logger.debug('One or more properties are missing from config.yaml, Using default params instead')
if len(dir_path) != len(scan_subdir):
raise Exception('dir_path and scan_dir should have the same number of values, exiting...')
except yaml.YAMLError as error:
logger.error('Error while parsing config.yaml', error)
sys.exit('Fatal Error, Exiting...')
except Exception as error:
logger.error(error)
sys.exit()
else:
print('config.yaml does not exist, Using default values')

class FilesNumberCollector(object):
def __init__(self):
pass
def getNumberOfFiles(self, path, scan_subdir):
if scan_subdir:
return sum([len(files) for r, d, files in os.walk(path)])
return len(os.listdir(path))

def collect(self):
gauge = GaugeMetricFamily('number_of_files', 'Number of Files in Directory', labels=["dir_path"])
for index, value in enumerate(dir_path):
if not os.path.exists(value):
logger.error('Directory ' + value + ' Does not exists, Skipping')
continue
logger.debug('Watching Dir: ' + value)
logger.debug('Watching Dir: ' + value + ' scan_subdir ' + str(scan_subdir[index]))
# Create a metric to track number of files.
gauge.add_metric([value], len(os.listdir(value)))
gauge.add_metric([value], self.getNumberOfFiles(value, scan_subdir[index]))
yield gauge

if __name__ == '__main__':
if os.path.exists('./config.yaml'):
logger.debug('Config file found')
with open('./config.yaml', 'r') as config_file:
try:
config = yaml.safe_load(config_file)
dir_path = config['dir_path'].split(',')
port = int(config['port'])
metric_freq = int(config['metric_freq'])
except yaml.YAMLError as error:
logger.error(error)
else:
print('config.yaml does not exist, Using default values')

# First Load config.yaml file and parse properties
loadConfig()
# Start up the server to expose the metrics.
start_http_server(port)
REGISTRY.register(FilesNumberCollector())
Expand Down

0 comments on commit 0cfe364

Please sign in to comment.