Skip to content

Commit

Permalink
Add OS filter to stale sensors sample. Closes #1024.
Browse files Browse the repository at this point in the history
  • Loading branch information
jshcodes committed Nov 23, 2023
1 parent 3edfb2b commit 4df3a47
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions samples/hosts/stale_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ def parse_command_line() -> object:
default=False,
action="store_true"
)
parser.add_argument(
"-f", "--filter-by-os",
help="OS filter (windows, macos, linux)",
default=None,
choices=["windows", "mac", "linux", "k8s"],
required=False,
dest="osfilter"
)
return parser.parse_args()


Expand All @@ -138,12 +146,16 @@ def get_host_details(id_list: list) -> list:
return returned


def get_hosts(date_filter: str, tag_filter: str) -> list:
def get_hosts(date_filter: str, tag_filter: str, os_filter: str) -> list:
"""Retrieve a list of hosts IDs that match the last_seen date filter."""
filter_string = f"last_seen:<='{date_filter}Z'"
if tag_filter:
filter_string = f"{filter_string} + tags:*'*{tag_filter}*'"

if os_filter:
os_filter = os_filter.title()
if os_filter == "K8s":
os_filter = "K8S"
filter_string = f"{filter_string} + platform_name:'{os_filter}'"
return falcon.query_devices_by_filter_scroll(
limit=5000,
filter=filter_string
Expand All @@ -161,7 +173,7 @@ def parse_host_detail(detail: dict, found: list):
now = datetime.now(timezone.utc)
then = dparser.parse(detail["last_seen"])
distance = (now - then).days
tagname = detail.get("tags", "Not Found")
tagname = detail.get("tags", "")
newtag = "\n".join(tagname)
found.append([
detail.get("hostname", "Unknown"),
Expand Down Expand Up @@ -201,7 +213,7 @@ def hide_hosts(id_list: list) -> dict:
stale = []
# For each stale host identified
try:
for host in get_host_details(get_hosts(STALE_DATE, args.tag)):
for host in get_host_details(get_hosts(STALE_DATE, args.tag, args.osfilter)):
# Retrieve host detail
stale = parse_host_detail(host, stale)
except KeyError as api_error:
Expand Down

0 comments on commit 4df3a47

Please sign in to comment.