Read a directory
Posted: Thu Mar 05, 2020 10:53 am
Code: Select all
tpl 1.14 module get_appfiledir_data;
metadata
tree_path := 'Custom','ComputerSystem hosted documents';
end metadata;
pattern getJsonFileDir 1.5
'''
Pattern for reading Json formatted data files on hosts.
This pattern reads all files in a directory where the extension is "json"
'''
metadata
origin := "CUSTOM"';
tkn_name := "Get List of APM Apps from Host";
end metadata;
overview
tags file;
end overview;
constants
filepathUnix := raw "/opt/schwabAppFiles";
filepathWin := raw "C:\myfolder\schwabAppFiles";
end constants;
triggers
on host := Host created, confirmed where os_class = "UNIX" or os_class = "Windows";
end triggers;
body
documenttype := none;
workingDirectory := none;
osSep := none;
basePath := none;
newAppFiles := [];
if host.os_class = "Windows" then
workingDirectoryListing := discovery.listDirectory(host, filepathWin);
osSep := "\\";
basePath := filepathWin + osSep;
else
workingDirectoryListing := discovery.listDirectory(host, filepathWin);
osSep := "/";
basePath := filepathUnix + osSep;
end if;
if workingDirectoryListing then
for f in workingDirectoryListing do
if f['file_type'] = "File" and f['size'] < 5120 and text.lower(f['name']) has substring ".json" then
appfile := discovery.fileGet(host, basePath + f['name']);
if appfile and appfile.content then
documenttype := "APMAppFileDirJSON";
file_table := table();
file_table["name"] := f['name'];
file_table["content"] := appfile.content;
file_table["type"] := documenttype;
list.append(newAppfiles, file_table);
end if;
end if;
end for;
host.appfiles := newAppFiles;
model.addDisplayAttribute(host, "appfiles");
end if
end body;
end pattern;