Skip to content

Commit 054198c

Browse files
authored
Merge pull request #353 from kurok/feature/json-output
feat: add native JSON output (-J/-j flags)
2 parents 4308799 + bb8bab1 commit 054198c

File tree

20 files changed

+627
-59
lines changed

20 files changed

+627
-59
lines changed

Lsof.8

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,47 @@ When specified without a PGID set that's all it does.
900900
.B \-H
901901
directs lsof to print human readable sizes, e.g. 123.4K 456.7M.
902902
.TP \w'names'u+4
903+
.B \-J
904+
selects JSON output mode. Instead of the traditional tabular or
905+
.B \-F
906+
field output, lsof produces a single JSON object on stdout containing
907+
a
908+
.B "processes"
909+
array. Each process object contains its fields and a
910+
.B "files"
911+
array of open file entries.
912+
.IP
913+
Field selection follows the same rules as
914+
.BR \-F :
915+
use
916+
.B \-F
917+
with field characters to select which fields appear in the JSON output.
918+
Without
919+
.BR \-F ,
920+
the default field set is used.
921+
.IP
922+
.B \-J
923+
is mutually exclusive with
924+
.B \-j
925+
and
926+
.BR \-t .
927+
Warnings and errors are sent to stderr; stdout is always valid JSON.
928+
.TP \w'names'u+4
929+
.B \-j
930+
selects JSON Lines output mode. Each open file produces one JSON
931+
object per line, combining process and file fields in a single
932+
denormalized record. This format is suitable for streaming pipelines,
933+
log ingestion (Splunk, Datadog, Elastic), and line\-oriented tools.
934+
.IP
935+
Field selection follows the same rules as
936+
.BR \-J .
937+
.IP
938+
.B \-j
939+
is mutually exclusive with
940+
.B \-J
941+
and
942+
.BR \-t .
943+
.TP \w'names'u+4
903944
.BI \-i " [i]"
904945
selects the listing of files any of whose Internet address
905946
matches the address specified in \fIi\fP.

Makefile.am

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ DIALECT_NEUTRAL_TESTS = tests/case-00-hello.bash \
132132
tests/case-20-offset-field.bash \
133133
tests/case-20-repeat-count.bash \
134134
tests/case-21-exit-Q-status.bash \
135-
tests/case-22-empty-process-name.bash
135+
tests/case-22-empty-process-name.bash \
136+
tests/case-30-json-output.bash \
137+
tests/case-31-jsonl-output.bash
136138
TESTS = $(DIALECT_NEUTRAL_TESTS)
137139
EXTRA_DIST += $(DIALECT_NEUTRAL_TESTS) \
138140
tests/case-13-classic.bash \

lib/common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,9 @@ extern int ErrStat;
684684
extern uid_t Euid;
685685
extern int Fcntx;
686686
extern int Ffield;
687+
extern int Fjson;
688+
extern int Fjsonl;
689+
extern int Fjson_first_proc;
687690
extern int Ffilesys;
688691
extern int Fhelp;
689692
extern int Fhost;

lib/dialects/aix/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ misc.o: ${HDR} misc.c
155155

156156
node.o: ${HDR} node.c
157157

158-
print.o: ${HDR} print.c
158+
print.o: ${HDR} version.h print.c
159159

160160
proc.o: ${HDR} proc.c
161161

lib/dialects/darwin/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ misc.o: ${HDR} misc.c
162162

163163
node.o: ${HDR} node.c
164164

165-
print.o: ${HDR} print.c
165+
print.o: ${HDR} version.h print.c
166166

167167
proc.o: ${HDR} proc.c
168168

lib/dialects/freebsd/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ misc.o: ${HDR} misc.c
141141

142142
node.o: ${HDR} node.c
143143

144-
print.o: ${HDR} print.c
144+
print.o: ${HDR} version.h print.c
145145

146146
proc.o: ${HDR} proc.c
147147

lib/dialects/hpux/kmem/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ misc.o: ${HDR} misc.c
147147

148148
node.o: ${HDR} node.c
149149

150-
print.o: ${HDR} print.c
150+
print.o: ${HDR} version.h print.c
151151

152152
proc.o: ${HDR} proc.c
153153

lib/dialects/hpux/pstat/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ misc.o: ${HDR} misc.c
136136

137137
node.o: ${HDR} node.c
138138

139-
print.o: ${HDR} print.c
139+
print.o: ${HDR} version.h print.c
140140

141141
proc.o: ${HDR} proc.c
142142

lib/dialects/linux/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ misc.o: ${HDR} misc.c
167167

168168
node.o: ${HDR} node.c
169169

170-
print.o: ${HDR} print.c
170+
print.o: ${HDR} version.h print.c
171171

172172
proc.o: ${HDR} proc.c
173173

lib/dialects/netbsd/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ misc.o: ${HDR} misc.c
145145

146146
node.o: ${HDR} node.c
147147

148-
print.o: ${HDR} print.c
148+
print.o: ${HDR} version.h print.c
149149

150150
proc.o: ${HDR} proc.c
151151

0 commit comments

Comments
 (0)