Compare desired and observed services
Distinguish service drift from missing observations before recommending a next step.
Desired and observed state answer different questions
A desired-state table describes what an administrator intends. An observation table describes what a collector saw. Join on the pair of asset ID and service name; joining only by service would mix identically named services on different machines.
This exercise recommends start or stop only when a supplied observation disagrees with desired state. A missing observation receives investigate. It is not proof that the service is stopped: the collector might have failed or omitted the machine.
Scope the comparison
Iterate desired rows so every managed service receives a decision. Extra observed services are outside this particular policy and do not become new requirements. Both tables have validated, unique composite keys. In real exports, duplicates or inconsistent collection times should be resolved before comparison.
Awk provides associative arrays for these lookups even though Bash 3.2 has no associative arrays. Use its composite-key separator rather than inventing a punctuation character that might appear in a name. Preserve desired and observed values alongside the proposed next step. No systemctl, launchctl, service restart or remote connection is performed by the browser exercise.
awk -F '\t' 'NR>1 {printf "%s/%s should be %s\n",$1,$2,$3}' desired.tsv
Put it into practice.
desired.tsv and observed.tsv each have header asset_id<TAB>service<TAB>state, unique asset/service pairs, nonempty names without ASCII control characters, and running or stopped states. Print asset_id<TAB>service<TAB>desired<TAB>observed<TAB>next_step for every desired row, sorted by asset then service in LC_ALL=C order. Matching states use none; observed stopped vs desired running uses start; observed running vs desired stopped uses stop. Missing observation uses missing/investigate. Ignore extra observed pairs. Produce recommendations only.
See an explained solution
export LC_ALL=C
printf 'asset_id\tservice\tdesired\tobserved\tnext_step\n'
awk -F '\t' 'FILENAME==ARGV[1] {if(FNR>1) observed[$1,$2]=$3;next}
FNR>1 {key=$1 SUBSEP $2;if(!(key in observed)){state="missing";step="investigate"}else{state=observed[key];step=(state==$3?"none":($3=="running"?"start":"stop"))}printf "%s\t%s\t%s\t%s\t%s\n",$1,$2,$3,state,step}
' observed.tsv desired.tsv | sort
The left join preserves every managed service while keeping missing observations separate from an actual stopped state. Recommendations remain tied to the correct asset/service pair and do not execute changes.