forked from PalisadoesFoundation/pattoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpattoo_ingesterd.py
executable file
·169 lines (133 loc) · 4.61 KB
/
pattoo_ingesterd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python3
"""Pattoo agent data cache ingester daemon.
Used to add data to backend database
"""
# Standard libraries
from __future__ import print_function
from time import sleep, time
import sys
import os
# Try to create a working PYTHONPATH
_BIN_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
_ROOT_DIRECTORY = os.path.abspath(os.path.join(_BIN_DIRECTORY, os.pardir))
_EXPECTED = '{0}pattoo{0}bin'.format(os.sep)
if _BIN_DIRECTORY.endswith(_EXPECTED) is True:
sys.path.append(_ROOT_DIRECTORY)
else:
print('''This script is not installed in the "{0}" directory. Please fix.\
'''.format(_EXPECTED))
sys.exit(2)
# Pattoo libraries
from pattoo_shared import log
from pattoo_shared import files as shared_files
from pattoo_shared.agent import Agent, AgentCLI
from pattoo.constants import PATTOO_INGESTERD_NAME, PATTOO_INGESTER_SCRIPT
from pattoo.configuration import ConfigIngester as Config
from pattoo import sysinfo
from pattoo.ingest import files
from pattoo.db.db import connectivity
class PollingAgent(Agent):
"""Agent that gathers data."""
def __init__(self, parent, config=None):
"""Initialize the class.
Args:
parent: Name of agent parent process
config: Config object
Returns:
None
"""
# Initialize key variables
Agent.__init__(self, parent, config=config)
def query(self):
"""Query all remote targets for data.
Args:
None
Returns:
None
"""
# Initialize key variables
use_script = False
_running = False
config = self.config
interval = config.ingester_interval()
script = '{}{}{}'.format(
_BIN_DIRECTORY, os.sep, PATTOO_INGESTER_SCRIPT)
# Post data to the remote server
while True:
# Get start time
ts_start = time()
# Check lockfile status
if use_script is True:
_running = check_lockfile()
# Process
if _running is False:
if bool(use_script) is True:
# Process cache with script
_result = shared_files.execute(script, die=False)
success = not bool(_result)
else:
# Process cache with function
success = files.process_cache()
if bool(success) is False:
log_message = ('''\
Ingester failed to run. Please check log files for possible causes.''')
log.log2warning(20029, log_message)
else:
log_message = ('''\
Ingester is unexpectedly still running. Check your parameters of error logs \
for possible causes.''')
log.log2warning(20129, log_message)
# Sleep. The duration could exceed the polling interval. Set sleep
# time to the polling interval when this occurs.
duration = time() - ts_start
if duration >= interval:
log_message = ('''Ingestion exceeded configured \
"ingester_interval" parameter by {:6.2f}s.'''.format(duration - interval))
sleep_time = 0
else:
sleep_time = abs(interval - duration)
log_message = (
'Ingester sleeping for {:6.2f}s.'.format(sleep_time))
log.log2info(20100, log_message)
sleep(sleep_time)
def check_lockfile():
"""Delete lockfile if found and ingester is not running.
Args:
None
Returns:
running: True if ingester script is running
"""
# Initialize key variables
agent_name = 'pattoo_ingester'
config = Config()
lockfile = shared_files.lock_file(agent_name, config)
# Script running
running = sysinfo.process_running(PATTOO_INGESTER_SCRIPT)
# Delete lockfile if found and ingester is not running.
# Caused by possible crash.
if os.path.exists(lockfile) is True and running is False:
os.remove(lockfile)
log_message = ('''\
Lock file {} found, but the {} script is not running\
'''.format(lockfile, PATTOO_INGESTER_SCRIPT))
log.log2warning(20030, log_message)
return running
def main():
"""Start the pattoo ingester daemon.
Args:
None
Returns:
None
"""
# Initialize key variables
config = Config()
# Make sure we have a database
_ = connectivity()
# Poll
agent_poller = PollingAgent(PATTOO_INGESTERD_NAME, config=config)
# Do control
cli = AgentCLI()
cli.control(agent_poller, graceful=True)
if __name__ == "__main__":
log.env()
main()