This repository has been archived by the owner on Sep 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathCrawlReport.java
277 lines (266 loc) · 9.44 KB
/
CrawlReport.java
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* Copyright (C) 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
* Name: CrawlReport.java
*
* Description: Command line application to count the number of urls crawled
* during the last 24h.
*
* The class uses the Google Admin API for java. This requires the appliance to
* be at least of software version 6.0. It will pull the information from Crawl
* Diagnostics and provide an overall value of files recrawled as well as a
* detailed list for each host.
*
* Please note that this class needs the following GData libraries:
* - gdata-core-1.0.jar
* - gdata-client-meta-1.0.jar
* - gdata-client-1.0.jar
* - gdata-gsa-meta-1.0.jar
* - gdata-gsa-1.0.jar
*
* The libraries can be downloaded from:
* http://code.google.com/p/google-enterprise-gdata-api/
*
* To run the program use the following two commands:
* 1) compile
* export LIBS="gdata-client-1.0.jar:gdata-client-meta-1.0.jar:gdata-core-1.0.jar:gdata-gsa-1.0.jar:gdata-gsa-meta-1.0.jar"
* javac -cp "$LIBS" CrawlReport.java
*
* 2) execute
* java -cp "$LIBS:./" CrawlReport --protocol=http --username=admin \
* --port=8000 --hostname=gsa.corp.com --password=secret
*
* Please note that the parameter settings are just examples. You will have to
* replace them with your local appliance values.
*
* This code is not supported by Google
*
*/
import com.google.enterprise.apis.client.GsaClient;
import com.google.enterprise.apis.client.GsaEntry;
import com.google.enterprise.apis.client.GsaFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* Command line application to count the number of urls crawled during the last
* 24h.
*
* The class uses the Google Admin API for java. This requires the appliance to
* be at least of software version 6.0.
*
* Usage Example: java CrawlReport --protocol http --hostname=gsa1 \
* --port=8000 --username=admin --password=secret
*
*/
public class CrawlReport {
/** the gdata client for the admin api. */
private static GsaClient myClient;
/** the map of hosts we counted. */
private static Map<String, Integer> hostmap = new HashMap<String, Integer>();
/** the map of parsed
* command line options. */
private static Map<String, String> optionMap = new HashMap<String, String>();
/** the unparsed map of command line options. */
private static List<String> argsList = new ArrayList<String>();
/** set of expected options. */
private static final String[] BASIC_OPTIONS =
{"protocol", "hostname", "port", "username", "password"};
/** number of required parameters. */
private static final int REQUIRED_ARGS_NUM = 5;
/** milliseconds of 24h. */
private static final long ONE_DAY_IN_MSEC = (24L * 3600L * 1000L);
/**
* unused constructor since we use only static methods, called from main.
*/
private CrawlReport() {
// unused -> empty.
}
/**
* The main entry point of the program.
*
* We enforce and parse command line args. Afterwards we establish the
* connection to the appliance and count docs in crawl diagnostics.
*
* @param args Command line parameters
*/
public static void main(final String[] args) {
if (args.length < REQUIRED_ARGS_NUM) {
usage();
return;
}
parseArgs(args);
try {
establishConnection();
System.out.println("Documents crawled during the last 24h: "
+ countDocsCrawledSinceYesterday());
System.out.println("\nDocuments crawled during the last 24h per host:");
Iterator<String> hostIterator = hostmap.keySet().iterator();
while (hostIterator.hasNext()) {
String host = hostIterator.next();
System.out.println(host + ": " + hostmap.get(host));
}
} catch (AuthenticationException e) {
System.err.println(e.getMessage());
e.printStackTrace();
} catch (MalformedURLException e) {
System.err.println(e.getMessage());
e.printStackTrace();
} catch (ServiceException e) {
System.err.println(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
}
/**
* tries to log into the Admin Console of the appliance.
*
* @throws AuthenticationException in case of password problem
*/
private static void establishConnection() throws AuthenticationException {
if (myClient == null) {
myClient =
new GsaClient(optionMap.get("protocol"), optionMap.get("hostname"),
Integer.parseInt(optionMap.get("port")),
optionMap.get("username"), optionMap.get("password"));
}
}
/**
* count the docs crawled during the last 24h.
*
* @return the number as a long.
* @throws IOException in case of IO problems during the API communication
* @throws ServiceException in case of errors during the service calls
*/
private static long countDocsCrawledSinceYesterday()
throws ServiceException, IOException {
// local vars:
Map<String, String> queries = new HashMap<String, String>();
Date nowMinus24h = new Date();
long docCounter = 0;
// connect to GSA Admin Console
if (myClient == null) {
establishConnection();
}
// initialize our time filter with the filter: now - 24h
nowMinus24h.setTime(new Date().getTime() - ONE_DAY_IN_MSEC);
// prepare our query to the admin api
int currentPage = 0;
int maxPages = 1; // we assume the worst case;
queries.put("sort", "host");
queries.put("flatList", "true");
/* queries.put("view","1"); // url status: 1 = crawled from remote host */
do {
currentPage++;
queries.put("pageNum", "" + currentPage);
// issue the query and iterate over the results
GsaFeed myFeed = myClient.queryFeed("diagnostics", queries);
for (GsaEntry entry : myFeed.getEntries()) {
// we are not interested in the overview
if (entry.getGsaContent("entryID").equals("description")) {
// update the num of pages:
maxPages = Integer.valueOf(entry.getGsaContent("numPages"));
} else if (entry.getGsaContent("type").equals("FileContentData")) {
Timestamp urlLastCrawlDate = new Timestamp(
Long.valueOf(entry.getGsaContent("timeStamp")) * 1000L);
if (urlLastCrawlDate.after(nowMinus24h)) {
// if we want to filter state state of the docs, we can use
// entry.getGsaContent("docState")
// the url is in entry.getGsaContent("entryID");
URL u = new URL(entry.getGsaContent("entryID"));
if (hostmap.get(u.getHost()) == null) {
hostmap.put(u.getHost(), 1);
} else {
hostmap.put(u.getHost(), hostmap.get(u.getHost()) + 1);
}
docCounter++;
}
}
}
} while (currentPage < maxPages);
return docCounter;
}
/**
* Parses the arguments from String array.
*
* Any argument should begin with '--' except the {@code command}, {@code
* feedName} and {@code entryName}. The {@code command} should be put in the
* first position of the command line arguments.
*
* @param args Arguments array
* @throws IllegalArgumentException in case of a non valid command line arg.
*/
private static void parseArgs(final String[] args)
throws IllegalArgumentException {
// parse arguments
for (String arg : args) {
if (arg.startsWith("--")) {
parseOption(arg.substring(2));
} else {
argsList.add(arg);
}
}
validateBasicOption();
}
/**
* Parses key and value pair from a string {@code argString}.
*
* The key-value pair will be restored in {@code optionMap}.
*
* @param argString the argument String
*/
private static void parseOption(final String argString) {
int index = argString.indexOf('=');
if (index == -1) {
optionMap.put(argString, null);
} else {
optionMap.put(argString.substring(0, index),
argString.substring(index + 1));
}
}
/**
* Validates options for creating {@code GsaClient}.
*
* @throws IllegalArgumentException
*/
private static void validateBasicOption() throws IllegalArgumentException {
for (String option : BASIC_OPTIONS) {
if (optionMap.get(option) == null) {
throw new IllegalArgumentException("Please specify --" + option);
}
}
}
/**
* Usage Description for the binary.
*/
private static void usage() {
System.err.println("Usage: CrawlReport <options> ");
System.err.println("options:\n --protocol:\n --hostname:\n --port:\n"
+ " --username:\n --password:\n ");
System.err.println("Example:\n CrawlReport --protocol=http "
+ "--hostname=gsa1 --port=8000 --username=user --password=password ");
}
}