-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgeneral_examples.sh
More file actions
162 lines (143 loc) · 5.61 KB
/
general_examples.sh
File metadata and controls
162 lines (143 loc) · 5.61 KB
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
#!/bin/bash
# The above line should be the shell you wish to execute this script.
#
# remote.it Example Script
# This has examples of the following (typically you will not being doing all of these at the same time):
# * graphQL mutations for Attributes (general attributes are displayed on the device detail page)
# * Setting Status columns with values that can be viewed in the portal and desktop applications
# * Has examples of usage of Environment variables
#
# This script first clears all the attributes for the device (diskfree, fwversion, uptime, vmstat, shell).
# Next, it collects specific system values from a device and sets them as attributes.
#
# Attribute diskfree = Free disk space, listed by partition
# Attribute fwversion = Linux version info from uname -a
# Attribute uptime = System uptime since last boot
# Attribute vmstat = List of all running processes
# Attribute shell = Shell version
#
# Finally, it sets the statusA field to complete with the current date and time. (e.g., "Complete: 2021-01-01 12:00:00")
#
# The following defines arguments for selecting a file, entering a string or selecting from a list of strings:
# These arguments are optional and can be removed if not needed.
# Once defined their values can be accessed via their name in the script (e.g., $textFile, $url, $name, $action)
# <arguments>, <type>, <name>, <prompt>, <option1>, <option2>, ...
# r3_argument, FileSelect, textFile, Select File, .txt
# r3_argument, StringEntry, url, Enter Fully-Qualified URL
# r3_argument, StringEntry, name, Enter a Name
# r3_argument, StringSelect, action, Choose Action, ADD, REMOVE
#
# These enivronment Variables are available to the script:
# JOB_DEVICE_ID - The job device ID of the device being managed
# GRAPHQL_API_PATH - The GraphQL API path to the remote.it api server
exec > ./external_script.log 2>&1
set -x # Start logging all commands executed
# A Function to set a single attribute
# These attribute values are displayed in the remote.it portal under the device's attributes
# $1 = attribute name (e.g., 'customAttribute')
# $2 = attribute value (e.g., 'some value')
Attribute() {
ATTRIBUTE_NAME="$1"
ATTRIBUTE_VALUE="$2"
# Send the data to the API, including jobDeviceId and attributeName in the URL
curl -X POST "https://${GRAPHQL_API_PATH}/job/attribute/$JOB_DEVICE_ID/$ATTRIBUTE_NAME" \
-H "Content-Type: text/plain" \
--data "$ATTRIBUTE_VALUE"
}
# A Function to set a single status (statusA, statusB, statusC, etc.)
# Status columns can be displayed on the device list
# $1 = status letter (e.g., 'a')
# $2 = status value (e.g., 'some value')
Status()
{
SHORTHAND=$(echo "$1" | tr '[:lower:]' '[:upper:]')
ATTRIBUTE_NAME="\$remoteit.status${SHORTHAND}"
ATTRIBUTE_VALUE="$2"
# Send the data to the API, including jobDeviceId and attributeName in the URL
curl -X POST "https://${GRAPHQL_API_PATH}/job/attribute/$JOB_DEVICE_ID/$ATTRIBUTE_NAME" \
-H "Content-Type: text/plain" \
--data "$ATTRIBUTE_VALUE"
}
# Function to Log data to a file
Log()
{
echo "$1" >> $0.log
}
# Clear all Attributes
Attribute diskfree ""
Attribute fwversion ""
Attribute uptime ""
Attribute vmstat ""
Attribute shell ""
Attribute textFile ""
Attribute url ""
Attribute name ""
Attribute action ""
Attribute fileContents ""
# Clear Status
Status a ""
#-------------------------------------------------
# retrieve the freed disk space
diskfree="$(df)"
Log "diskfree: $diskfree"
# send to status column a in remote.it portal
Attribute diskfree "$diskfree"
#-------------------------------------------------
#-------------------------------------------------
# retrieve the Linux kernel version
fwversion="$(uname -a)"
Log "fwversion: $fwversion"
# send to status column b in remote.it portal
Attribute fwversion "$fwversion"
#-------------------------------------------------
#-------------------------------------------------
# retrieve the system uptime
uptime="$(uptime)"
Log "uptime: $uptime"
# send to status column c in remote.it portal
Attribute uptime "$uptime"
#-------------------------------------------------
#-------------------------------------------------
# get the list of running processes
# Check if vm_stat is available (common on macOS)
if command -v vm_stat &> /dev/null; then
vmstat_output="$(vm_stat)"
# Otherwise, check if vmstat is available (common on Linux)
elif command -v vmstat &> /dev/null; then
vmstat_output="$(vmstat)"
else
# If neither command is available, set vmstat_output to an error message
vmstat_output="Neither vm_stat nor vmstat is available on this system."
fi
Log "vmstat: $vmstat_output"
# set attribute for vmstat
Attribute vmstat "$vmstat_output"
#-------------------------------------------------
#-------------------------------------------------
# use the shell variable $SHELL to get the shell version
Log "shell: $SHELL"
# set attribute for shell
Attribute shell "$SHELL"
#-------------------------------------------------
#-------------------------------------------------
# Set status A to "complete: date and time"
Status a "Complete: $(date)"
# Echo all inputs to the log file
Log "JOB_DEVICE_ID: $JOB_DEVICE_ID"
Log "GRAPHQL_API_PATH: $GRAPHQL_API_PATH"
Log "textFile: $textFile"
Attribute textFile "$textFile"
Log "url: $url"
Attribute url "$url"
Log "name: $name"
Attribute name "$name"
Log "action: $action"
Attribute action "$action"
# Log contents of the file named in fileName
Log "textFile File Contents: $(cat $textFile)"
Attribute fileContents "$(cat $textFile)"
#=======================================================================
# Lastly finalize job, no updates allowed after this
exit 0
# Use this line in case of error
# exit 1