Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Gelencsér Szabolcs
/
monitor-client
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Wiki
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
f1892912
authored
Jan 26, 2014
by
Gregory Nagy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refomated code using pep8 conventions.
manage.py: error handling added
parent
99be2c47
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
124 additions
and
133 deletions
+124
-133
manage.py
+11
-15
src/client.py
+1
-1
src/cnfparse.py
+25
-25
src/collectables.py
+11
-11
src/metrics.py
+76
-81
No files found.
manage.py
View file @
f1892912
import
sys
import
sys
from
src
import
cnfparse
,
client
,
collectables
from
src
import
cnfparse
,
client
,
collectables
def
main
():
def
main
():
if
len
(
sys
.
argv
)
<
2
:
print
(
"usage: manage.py run [options]"
)
if
(
len
(
sys
.
argv
)
<
2
):
if
len
(
sys
.
argv
)
is
not
2
and
sys
.
argv
[
1
]
is
not
"run"
:
print
(
"usage: manage.py run [options]"
)
print
(
"[ERROR] Command cannot be parsed. Exiting..."
)
print
(
"""
return
options:
--config <path> : path to the configuration file you intend
to start the client with
--debug : enables the debug mode and writes metrics sent to the
server
"""
)
if
(
len
(
sys
.
argv
)
==
2
and
sys
.
argv
[
1
]
==
"run"
):
configuration
,
metrics
=
cnfparse
.
importConf
(
"config/client.conf"
)
configuration
,
metrics
=
cnfparse
.
importConf
(
"config/client.conf"
)
cli
=
client
.
Client
(
configuration
)
cli
.
startReporting
(
metricCollectors
=
collectables
.
collectables
.
provide
(
metrics
))
cli
=
client
.
Client
(
configuration
)
cli
.
startReporting
(
metricCollectors
=
collectables
.
collectables
.
provide
(
metrics
))
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
main
()
main
()
src/client.py
View file @
f1892912
...
@@ -84,7 +84,7 @@ class Client:
...
@@ -84,7 +84,7 @@ class Client:
"""
"""
try
:
try
:
self
.
channel
.
basic_publish
(
exchange
=
self
.
amqp_queue
,
self
.
channel
.
basic_publish
(
exchange
=
self
.
amqp_queue
,
routing_key
=
''
,
body
=
"
\n
"
.
join
(
message
))
routing_key
=
''
,
body
=
"
\n
"
.
join
(
message
))
return
True
return
True
except
:
except
:
print
(
"[ERROR] An error has occured while sending metrics to the "
print
(
"[ERROR] An error has occured while sending metrics to the "
...
...
src/cnfparse.py
View file @
f1892912
...
@@ -2,29 +2,29 @@ import ConfigParser as configparser
...
@@ -2,29 +2,29 @@ import ConfigParser as configparser
def
importConf
(
path_to_file
):
def
importConf
(
path_to_file
):
config
=
configparser
.
RawConfigParser
(
allow_no_value
=
False
)
config
=
configparser
.
RawConfigParser
(
allow_no_value
=
False
)
try
:
try
:
config
.
read
(
path_to_file
)
config
.
read
(
path_to_file
)
params
=
{}
params
=
{}
metrics
=
{}
metrics
=
{}
params
[
"debugMode"
]
=
config
.
get
(
"Client"
,
"Debug"
)
params
[
"debugMode"
]
=
config
.
get
(
"Client"
,
"Debug"
)
metrics
[
"cpu.usage"
]
=
int
(
config
.
get
(
"Metrics"
,
"cpuUsage"
))
metrics
[
"cpu.usage"
]
=
int
(
config
.
get
(
"Metrics"
,
"cpuUsage"
))
metrics
[
"memory.usage"
]
=
int
(
config
.
get
(
"Metrics"
,
"memoryUsage"
))
metrics
[
"memory.usage"
]
=
int
(
config
.
get
(
"Metrics"
,
"memoryUsage"
))
metrics
[
"user.count"
]
=
int
(
config
.
get
(
"Metrics"
,
"userCount"
))
metrics
[
"user.count"
]
=
int
(
config
.
get
(
"Metrics"
,
"userCount"
))
metrics
[
"swap.usage"
]
=
int
(
config
.
get
(
"Metrics"
,
"swapUsage"
))
metrics
[
"swap.usage"
]
=
int
(
config
.
get
(
"Metrics"
,
"swapUsage"
))
metrics
[
"system.boot_time"
]
=
int
(
config
.
get
(
"Metrics"
,
metrics
[
"system.boot_time"
]
=
int
(
config
.
get
(
"Metrics"
,
"systemBootTime"
))
"systemBootTime"
))
metrics
[
"network"
]
=
int
(
config
.
get
(
"Metrics"
,
"dataTraffic"
))
metrics
[
"network"
]
=
int
(
config
.
get
(
"Metrics"
,
"dataTraffic"
))
params
[
"kvmCpuUsage"
]
=
int
(
config
.
get
(
"KVM"
,
"cpuUsage"
))
params
[
"kvmCpuUsage"
]
=
int
(
config
.
get
(
"KVM"
,
"cpuUsage"
))
params
[
"kvmMemoryUsage"
]
=
int
(
config
.
get
(
"KVM"
,
"memoryUsage"
))
params
[
"kvmMemoryUsage"
]
=
int
(
config
.
get
(
"KVM"
,
"memoryUsage"
))
except
configparser
.
NoSectionError
:
except
configparser
.
NoSectionError
:
print
(
"Config file contains error! Reason: Missing section."
)
print
(
"Config file contains error! Reason: Missing section."
)
raise
raise
except
configparser
.
ParsingError
:
except
configparser
.
ParsingError
:
print
(
"Config file contains error! Reason: Cannot parse."
)
print
(
"Config file contains error! Reason: Cannot parse."
)
raise
raise
except
configparser
.
MissingSectionHeaderError
:
except
configparser
.
MissingSectionHeaderError
:
print
(
"Config file contains error! Reason: Missing section-header."
)
print
(
"Config file contains error! Reason: Missing section-header."
)
raise
raise
return
params
,
metrics
return
params
,
metrics
src/collectables.py
View file @
f1892912
...
@@ -3,17 +3,17 @@ from metrics import *
...
@@ -3,17 +3,17 @@ from metrics import *
class
collectables
:
class
collectables
:
__collectables
=
{
__collectables
=
{
std
.
cpu
.
usage
.
name
:
[
std
.
cpu
.
usage
],
std
.
cpu
.
usage
.
name
:
[
std
.
cpu
.
usage
],
std
.
memory
.
usage
.
name
:
[
std
.
memory
.
usage
],
std
.
memory
.
usage
.
name
:
[
std
.
memory
.
usage
],
std
.
swap
.
usage
.
name
:
[
std
.
swap
.
usage
],
std
.
swap
.
usage
.
name
:
[
std
.
swap
.
usage
],
std
.
user
.
count
.
name
:
[
std
.
user
.
count
],
std
.
user
.
count
.
name
:
[
std
.
user
.
count
],
std
.
network
.
packages_sent
.
name
:
[
std
.
network
.
packages_sent
],
std
.
network
.
packages_sent
.
name
:
[
std
.
network
.
packages_sent
],
std
.
network
.
packages_received
.
name
:
[
std
.
network
.
packages_received
],
std
.
network
.
packages_received
.
name
:
[
std
.
network
.
packages_received
],
std
.
network
.
bytes_sent
.
name
:
[
std
.
network
.
bytes_sent
],
std
.
network
.
bytes_sent
.
name
:
[
std
.
network
.
bytes_sent
],
std
.
network
.
bytes_received
.
name
:
[
std
.
network
.
bytes_received
],
std
.
network
.
bytes_received
.
name
:
[
std
.
network
.
bytes_received
],
std
.
system
.
boot_time
.
name
:
[
std
.
system
.
boot_time
],
std
.
system
.
boot_time
.
name
:
[
std
.
system
.
boot_time
],
"network"
:
[
std
.
network
.
bytes_sent
,
std
.
network
.
bytes_received
,
"network"
:
[
std
.
network
.
bytes_sent
,
std
.
network
.
bytes_received
,
std
.
network
.
packages_sent
,
std
.
network
.
packages_received
],
std
.
network
.
packages_sent
,
std
.
network
.
packages_received
],
}
}
@staticmethod
@staticmethod
...
...
src/metrics.py
View file @
f1892912
...
@@ -8,87 +8,82 @@ import collections
...
@@ -8,87 +8,82 @@ import collections
Metrics
=
collections
.
namedtuple
(
"Metrics"
,
[
"name"
,
"value"
])
Metrics
=
collections
.
namedtuple
(
"Metrics"
,
[
"name"
,
"value"
])
class
Collection
(
object
):
class
Collection
(
object
):
class
Group
(
object
):
class
Group
(
object
):
class
Metric
(
object
):
class
Metric
(
object
):
name
=
"unknown"
name
=
"unknown"
collector_function
=
0
collector_function
=
0
collector_function_arguments
=
{}
collector_function_arguments
=
{}
collector_function_result_attr
=
""
collector_function_result_attr
=
""
@classmethod
@classmethod
def
harvest
(
cls
):
def
harvest
(
cls
):
query
=
cls
.
collector_function
.
im_func
(
**
cls
.
collector_function_arguments
)
query
=
cls
.
collector_function
.
im_func
(
if
((
isinstance
(
query
,
list
))
or
(
isinstance
(
query
,
dict
))):
**
cls
.
collector_function_arguments
)
return
Metrics
(
cls
.
name
,
if
((
isinstance
(
query
,
list
))
or
(
isinstance
(
query
,
dict
))):
query
[
cls
.
collector_function_result_attr
])
return
Metrics
(
cls
.
name
,
elif
(
isinstance
(
query
,
tuple
)):
query
[
cls
.
collector_function_result_attr
])
return
Metrics
(
cls
.
name
,
elif
(
isinstance
(
query
,
tuple
)):
query
.
__getattribute__
(
cls
.
collector_function_result_attr
))
return
Metrics
(
cls
.
name
,
else
:
query
.
__getattribute__
(
return
Metrics
(
cls
.
name
,
query
)
cls
.
collector_function_result_attr
))
else
:
return
Metrics
(
cls
.
name
,
query
)
##############################################################################
##############################################################################
class
std
(
Collection
):
class
std
(
Collection
):
class
cpu
(
Collection
.
Group
):
class
cpu
(
Collection
.
Group
):
class
usage
(
Collection
.
Group
.
Metric
):
name
=
"cpu.usage"
class
usage
(
Collection
.
Group
.
Metric
):
collector_function
=
ps
.
cpu_percent
name
=
"cpu.usage"
collector_function_arguments
=
{
collector_function
=
ps
.
cpu_percent
'interval'
:
0.0
,
collector_function_arguments
=
{
}
'interval'
:
0.0
,
}
class
memory
(
Collection
.
Group
):
class
usage
(
Collection
.
Group
.
Metric
):
class
memory
(
Collection
.
Group
):
name
=
"memory.usage"
collector_function
=
ps
.
virtmem_usage
class
usage
(
Collection
.
Group
.
Metric
):
collector_function_result_attr
=
"percent"
name
=
"memory.usage"
collector_function
=
ps
.
virtmem_usage
class
swap
(
Collection
.
Group
):
collector_function_result_attr
=
"percent"
class
usage
(
Collection
.
Group
.
Metric
):
name
=
"swap.usage"
class
swap
(
Collection
.
Group
):
collector_function
=
ps
.
swap_memory
collector_function_result_attr
=
"percent"
class
usage
(
Collection
.
Group
.
Metric
):
name
=
"swap.usage"
class
user
(
Collection
.
Group
):
collector_function
=
ps
.
swap_memory
class
count
(
Collection
.
Group
.
Metric
):
collector_function_result_attr
=
"percent"
name
=
"user.count"
class
user
(
Collection
.
Group
):
@classmethod
def
harvest
(
cls
):
class
count
(
Collection
.
Group
.
Metric
):
return
Metrics
(
cls
.
name
,
len
(
ps
.
get_users
()))
name
=
"user.count"
class
network
(
Collection
.
Group
):
@classmethod
class
packages_sent
(
Collection
.
Group
.
Metric
):
def
harvest
(
cls
):
name
=
"network.packages_sent"
return
Metrics
(
cls
.
name
,
len
(
ps
.
get_users
()))
collector_function
=
ps
.
network_io_counters
collector_function_result_attr
=
"packets_sent"
class
network
(
Collection
.
Group
):
class
packages_received
(
Collection
.
Group
.
Metric
):
class
packages_sent
(
Collection
.
Group
.
Metric
):
name
=
"network.packages_received"
name
=
"network.packages_sent"
collector_function
=
ps
.
network_io_counters
collector_function
=
ps
.
network_io_counters
collector_function_result_attr
=
"packets_recv"
collector_function_result_attr
=
"packets_sent"
class
bytes_sent
(
Collection
.
Group
.
Metric
):
class
packages_received
(
Collection
.
Group
.
Metric
):
name
=
"network.bytes_sent"
name
=
"network.packages_received"
collector_function
=
ps
.
network_io_counters
collector_function
=
ps
.
network_io_counters
collector_function_result_attr
=
"bytes_sent"
collector_function_result_attr
=
"packets_recv"
class
bytes_received
(
Collection
.
Group
.
Metric
):
class
bytes_sent
(
Collection
.
Group
.
Metric
):
name
=
"network.bytes_received"
name
=
"network.bytes_sent"
collector_function
=
ps
.
network_io_counters
collector_function
=
ps
.
network_io_counters
collector_function_result_attr
=
"bytes_recv"
collector_function_result_attr
=
"bytes_sent"
class
system
(
Collection
.
Group
):
class
bytes_received
(
Collection
.
Group
.
Metric
):
class
boot_time
(
Collection
.
Group
.
Metric
):
name
=
"network.bytes_received"
name
=
"system.boot_time"
collector_function
=
ps
.
network_io_counters
collector_function
=
ps
.
get_boot_time
collector_function_result_attr
=
"bytes_recv"
\ No newline at end of file
class
system
(
Collection
.
Group
):
class
boot_time
(
Collection
.
Group
.
Metric
):
name
=
"system.boot_time"
collector_function
=
ps
.
get_boot_time
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment