├── .dockerignore ├── .gitignore ├── grafana.png ├── BUILD ├── WORKSPACE ├── Makefile ├── Dockerfile ├── repositories.bzl ├── src ├── connection_table.h ├── connection.h ├── connection_table.cc ├── main.cc └── connection.cc ├── README.md └── LICENSE /.dockerignore: -------------------------------------------------------------------------------- 1 | .git 2 | bazel-* 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | bazel-* 2 | conntrack_exporter 3 | TODO 4 | -------------------------------------------------------------------------------- /grafana.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hiveco/conntrack_exporter/HEAD/grafana.png -------------------------------------------------------------------------------- /BUILD: -------------------------------------------------------------------------------- 1 | cc_binary( 2 | name = "conntrack_exporter", 3 | srcs = glob(["src/*.cc", "src/*h"]), 4 | deps = [ 5 | "@com_github_jupp0r_prometheus_cpp//pull", 6 | "@libnetfilter_conntrack//:libnetfilter_conntrack", 7 | "@argagg//:argagg", 8 | ], 9 | linkstatic=1, 10 | linkopts = [ 11 | "-l netfilter_conntrack", 12 | ], 13 | ) 14 | -------------------------------------------------------------------------------- /WORKSPACE: -------------------------------------------------------------------------------- 1 | load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file") 2 | http_archive( 3 | name = "com_github_jupp0r_prometheus_cpp", 4 | strip_prefix = "prometheus-cpp-master", 5 | urls = ["https://github.com/jupp0r/prometheus-cpp/archive/master.zip"], 6 | ) 7 | 8 | load("@com_github_jupp0r_prometheus_cpp//bazel:repositories.bzl", "prometheus_cpp_repositories") 9 | 10 | prometheus_cpp_repositories() 11 | 12 | load("//:repositories.bzl", "conntrack_exporter_dependencies") 13 | conntrack_exporter_dependencies() 14 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CONNTRACK_EXPORTER_VERSION = 0.3.1 2 | 3 | build: 4 | bazel build -c dbg //:conntrack_exporter 5 | cp -f bazel-bin/conntrack_exporter . 6 | 7 | build_stripped: 8 | bazel build --strip=always -c opt //:conntrack_exporter 9 | cp -f bazel-bin/conntrack_exporter . 10 | 11 | # May need to run make via sudo for this: 12 | run: 13 | ./conntrack_exporter 14 | 15 | build_docker: 16 | docker build -t hiveco/conntrack_exporter:$(CONNTRACK_EXPORTER_VERSION) --target release . 17 | 18 | run_docker: build_docker 19 | docker run -it --rm \ 20 | --cap-add=NET_ADMIN \ 21 | --name=conntrack_exporter \ 22 | --net=host \ 23 | hiveco/conntrack_exporter:$(CONNTRACK_EXPORTER_VERSION) 24 | 25 | publish_docker: build_docker 26 | docker tag hiveco/conntrack_exporter:$(CONNTRACK_EXPORTER_VERSION) hiveco/conntrack_exporter:latest 27 | docker push hiveco/conntrack_exporter:$(CONNTRACK_EXPORTER_VERSION) 28 | docker push hiveco/conntrack_exporter:latest 29 | 30 | clean: 31 | bazel clean 32 | rm -f conntrack_exporter 33 | 34 | .PHONY: build build_stripped run build_docker run_docker publish_docker clean 35 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # BUILDER IMAGE 2 | 3 | FROM gcr.io/bazel-public/bazel:6.4.0 AS builder 4 | 5 | USER root 6 | 7 | RUN set -ex; \ 8 | apt-get update -qq; \ 9 | DEBIAN_FRONTEND=noninteractive apt-get install -qqy --no-install-recommends \ 10 | libnetfilter-conntrack-dev 11 | 12 | WORKDIR /src 13 | ADD . /src/ 14 | 15 | 16 | # DEBUG BUILD 17 | 18 | FROM builder AS build_debug 19 | 20 | RUN bazel build -c dbg //:conntrack_exporter 21 | 22 | 23 | # RELEASE BUILD 24 | 25 | FROM builder AS build_release 26 | 27 | RUN bazel build --strip=always -c opt //:conntrack_exporter 28 | 29 | 30 | # BASE IMAGE 31 | 32 | FROM ubuntu:22.04 AS base 33 | 34 | RUN set -ex; \ 35 | apt-get update -qq; \ 36 | DEBIAN_FRONTEND=noninteractive apt-get install -qqy --no-install-recommends \ 37 | libnetfilter-conntrack-dev 38 | 39 | ENTRYPOINT ["conntrack_exporter"] 40 | 41 | 42 | # DEBUG IMAGE 43 | 44 | FROM base AS debug 45 | 46 | COPY --from=build_debug /src/bazel-bin/conntrack_exporter /bin/ 47 | 48 | 49 | # RELEASE IMAGE 50 | 51 | FROM base AS release 52 | 53 | COPY --from=build_release /src/bazel-bin/conntrack_exporter /bin/ 54 | -------------------------------------------------------------------------------- /repositories.bzl: -------------------------------------------------------------------------------- 1 | load("@bazel_tools//tools/build_defs/repo:git.bzl", "new_git_repository") 2 | 3 | def libnetfilter_conntrack_repositories(): 4 | 5 | BUILD = """ 6 | cc_library( 7 | name = "libnetfilter_conntrack", 8 | includes = ["."], 9 | visibility = ["//visibility:public"], 10 | ) 11 | """ 12 | 13 | # This requires libnetfilter-conntrack-dev to be installed on Ubuntu/Debian 14 | native.new_local_repository( 15 | name = "libnetfilter_conntrack", 16 | path = "/usr/include", 17 | build_file_content = BUILD, 18 | ) 19 | 20 | 21 | def argagg_repositories(): 22 | 23 | BUILD = """ 24 | cc_library( 25 | name = "argagg", 26 | hdrs = ["include/argagg/argagg.hpp"], 27 | includes = ["include"], 28 | visibility = ["//visibility:public"], 29 | ) 30 | """ 31 | 32 | new_git_repository( 33 | name = "argagg", 34 | remote = "https://github.com/vietjtnguyen/argagg.git", 35 | commit = "4c8c86180cfafb1448f583ed0973da8c2f559dd6", 36 | build_file_content = BUILD, 37 | ) 38 | 39 | 40 | def conntrack_exporter_dependencies(): 41 | libnetfilter_conntrack_repositories() 42 | argagg_repositories() 43 | -------------------------------------------------------------------------------- /src/connection_table.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include "connection.h" 6 | 7 | 8 | namespace conntrackex { 9 | 10 | using namespace std; 11 | 12 | typedef list ConnectionList; 13 | 14 | class ConnectionTable 15 | { 16 | public: 17 | 18 | ConnectionTable(); 19 | ~ConnectionTable(); 20 | 21 | void enableLogging(bool enable = true) { this->log_events = enable; } 22 | void enableDebugging(bool enable = true) { this->debugging = enable; } 23 | void setLoggingFormat(string format) { this->log_events_format = format; } 24 | void addIgnoredHost(const string& host) { this->ignored_hosts.push_back(host); } 25 | 26 | void attach(); 27 | void update(); 28 | 29 | const ConnectionList& getConnections() const { return this->connections; } 30 | 31 | private: 32 | 33 | nfct_handle* makeConntrackHandle(); 34 | void rebuild(); 35 | void updateConnection(enum nf_conntrack_msg_type type, Connection& connection); 36 | bool isIgnoredHost(const string& host) const; 37 | 38 | static int nfct_callback_attach(enum nf_conntrack_msg_type type, struct nf_conntrack* ct, void* data); 39 | static int nfct_callback_rebuild(enum nf_conntrack_msg_type type, struct nf_conntrack* ct, void* data); 40 | static int nfct_callback_dummy(enum nf_conntrack_msg_type type, struct nf_conntrack* ct, void* data) { return NFCT_CB_STOP; } 41 | 42 | nfct_handle* attach_handle; 43 | nfct_handle* rebuild_handle; 44 | bool is_rebuilding; 45 | bool log_events = false; 46 | string log_events_format = "netfilter"; 47 | bool debugging = false; 48 | ConnectionList connections; 49 | list ignored_hosts; 50 | }; 51 | 52 | } // namespace conntrackex 53 | -------------------------------------------------------------------------------- /src/connection.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | 11 | namespace conntrackex { 12 | 13 | using namespace std; 14 | 15 | enum class ConnectionState : unsigned char 16 | { 17 | OPENING, 18 | OPEN, 19 | CLOSING, 20 | CLOSED 21 | }; 22 | 23 | class Connection 24 | { 25 | public: 26 | 27 | Connection(nf_conntrack* ct); 28 | Connection(const Connection &other); 29 | ~Connection(); 30 | 31 | static void loadLocalIPAddresses(bool log_debug_messages = false); 32 | 33 | string getOriginalSourceIP() const; 34 | uint16_t getOriginalSourcePort() const; 35 | string getOriginalSourceHost() const { return this->getOriginalSourceIP() + ":" + to_string(this->getOriginalSourcePort()); } 36 | string getOriginalDestinationIP() const; 37 | uint16_t getOriginalDestinationPort() const; 38 | string getOriginalDestinationHost() const { return this->getOriginalDestinationIP() + ":" + to_string(this->getOriginalDestinationPort()); } 39 | string getReplySourceIP() const; 40 | uint16_t getReplySourcePort() const; 41 | string getReplySourceHost() const { return this->getReplySourceIP() + ":" + to_string(this->getReplySourcePort()); } 42 | string getReplyDestinationIP() const; 43 | uint16_t getReplyDestinationPort() const; 44 | string getReplyDestinationHost() const { return this->getReplyDestinationIP() + ":" + to_string(this->getReplyDestinationPort()); } 45 | 46 | string getRemoteHost() const; 47 | 48 | bool hasState() const; 49 | ConnectionState getState() const; 50 | string getStateString() const { return stateToString(this->getState()); } 51 | 52 | void setEventType(nf_conntrack_msg_type type) { this->event_type = type; } 53 | string toString() const; 54 | string toNetFilterString() const; 55 | 56 | bool operator==(const Connection& other) const; 57 | 58 | private: 59 | 60 | static string ip32ToString(uint32_t ip32); 61 | static string stateToString(const ConnectionState state); 62 | bool hasEventType() const { return this->event_type != NFCT_T_UNKNOWN; } 63 | const string getEventTypeString() const; 64 | 65 | static bool isLocalIPAddress(const string& ip_address); 66 | static list local_ip_addresses; 67 | 68 | nf_conntrack* conntrack; 69 | nf_conntrack_msg_type event_type = NFCT_T_UNKNOWN; 70 | }; 71 | 72 | } // namespace conntrackex 73 | -------------------------------------------------------------------------------- /src/connection_table.cc: -------------------------------------------------------------------------------- 1 | #include "connection_table.h" 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | namespace conntrackex { 9 | 10 | using namespace std; 11 | 12 | ConnectionTable::ConnectionTable() 13 | { 14 | this->attach_handle = makeConntrackHandle(); 15 | this->rebuild_handle = makeConntrackHandle(); 16 | } 17 | 18 | ConnectionTable::~ConnectionTable() 19 | { 20 | nfct_close(this->attach_handle); 21 | nfct_close(this->rebuild_handle); 22 | } 23 | 24 | nfct_handle* ConnectionTable::makeConntrackHandle() 25 | { 26 | uint32_t family = AF_INET; 27 | 28 | // HACK: Initialize conntrack; see https://github.com/markusa/netsniff-ng_filter/blob/master/src/flowtop.c#L850-L861 29 | auto dummy_handle = nfct_open(NFNL_SUBSYS_CTNETLINK, NFCT_ALL_CT_GROUPS); 30 | if (!dummy_handle) 31 | throw runtime_error("Unable to open NetFilter socket. (Does the current user have sufficient privileges?)"); 32 | nfct_callback_register(dummy_handle, NFCT_T_ALL, ConnectionTable::nfct_callback_dummy, NULL); 33 | nfct_query(dummy_handle, NFCT_Q_DUMP, &family); 34 | nfct_close(dummy_handle); 35 | 36 | auto handle = nfct_open(NFNL_SUBSYS_CTNETLINK, NFCT_ALL_CT_GROUPS); 37 | if (!handle) 38 | throw runtime_error("Unable to open NetFilter socket. (Does the current user have sufficient privileges?)"); 39 | 40 | nfct_query(handle, NFCT_Q_FLUSH, &family); 41 | 42 | auto filter = nfct_filter_create(); 43 | if (!filter) 44 | throw runtime_error("Unable to create netfilter_conntrack filter!"); 45 | 46 | // Filter in only TCP entries: 47 | nfct_filter_add_attr_u32(filter, NFCT_FILTER_L4PROTO, IPPROTO_TCP); 48 | 49 | if (nfct_filter_attach(nfct_fd(handle), filter) < 0) 50 | throw runtime_error("Unable to attach netfilter_conntrack filter to handle!"); 51 | 52 | nfct_filter_destroy(filter); 53 | 54 | return handle; 55 | } 56 | 57 | void ConnectionTable::attach() 58 | { 59 | // Switch the netfilter socket to non-blocking to prevent nfct_catch from 60 | // taking control. See https://www.spinics.net/lists/netfilter-devel/msg20952.html 61 | int fd = nfct_fd(this->attach_handle); 62 | int flags = fcntl(fd, F_GETFL, 0); 63 | flags |= O_NONBLOCK; 64 | if (fcntl(fd, F_SETFL, flags) != 0) 65 | throw runtime_error("Error setting the NetFilter socket to non-blocking mode."); 66 | 67 | this->rebuild(); 68 | 69 | nfct_callback_register(this->attach_handle, NFCT_T_ALL, ConnectionTable::nfct_callback_attach, this); 70 | } 71 | 72 | void ConnectionTable::update() 73 | { 74 | nfct_catch(this->attach_handle); 75 | } 76 | 77 | void ConnectionTable::rebuild() 78 | { 79 | this->connections.clear(); 80 | 81 | nfct_callback_register(this->rebuild_handle, NFCT_T_ALL, ConnectionTable::nfct_callback_rebuild, this); 82 | 83 | this->is_rebuilding = true; 84 | if (this->debugging) 85 | cout << "[DEBUG] Rebuilding connection table" << endl; 86 | 87 | uint32_t family = AF_INET; 88 | nfct_query(this->rebuild_handle, NFCT_Q_DUMP, &family); 89 | 90 | this->is_rebuilding = false; 91 | if (this->debugging) 92 | cout << "[DEBUG] Finished rebuilding connection table" << endl; 93 | } 94 | 95 | bool ConnectionTable::isIgnoredHost(const string& host) const 96 | { 97 | return (find(this->ignored_hosts.begin(), 98 | this->ignored_hosts.end(), 99 | host) != this->ignored_hosts.end()); 100 | } 101 | 102 | int ConnectionTable::nfct_callback_attach(enum nf_conntrack_msg_type type, struct nf_conntrack* ct, void* data) 103 | { 104 | Connection connection(ct); 105 | auto table = static_cast(data); 106 | table->updateConnection(type, connection); 107 | 108 | return NFCT_CB_CONTINUE; 109 | } 110 | 111 | int ConnectionTable::nfct_callback_rebuild(enum nf_conntrack_msg_type type, struct nf_conntrack* ct, void* data) 112 | { 113 | if (type == NFCT_T_UPDATE) 114 | type = NFCT_T_NEW; 115 | 116 | Connection connection(ct); 117 | auto table = static_cast(data); 118 | table->updateConnection(type, connection); 119 | 120 | return NFCT_CB_CONTINUE; 121 | } 122 | 123 | void ConnectionTable::updateConnection(enum nf_conntrack_msg_type type, Connection& connection) 124 | { 125 | connection.setEventType(type); 126 | 127 | if (this->isIgnoredHost(connection.getRemoteHost())) 128 | { 129 | if (this->debugging) 130 | { 131 | cout << "[DEBUG] Remote host is present on the ignore list, ignoring connection:" << endl; 132 | cout << "\t" << connection.toNetFilterString() << endl; 133 | } 134 | return; 135 | } 136 | 137 | // Log the event: 138 | if (this->log_events && !this->is_rebuilding) 139 | { 140 | if (this->log_events_format == "netfilter") 141 | cout << connection.toNetFilterString() << endl; 142 | else 143 | cout << connection.toString() << endl; 144 | } 145 | 146 | // If we can find an existing connection in our table that matches the 147 | // incoming one, delete it: 148 | auto old_connection_it = find(this->connections.begin(), this->connections.end(), connection); 149 | bool exists = (old_connection_it != this->connections.end()); 150 | if (exists) 151 | { 152 | if (this->debugging) 153 | { 154 | cout << "[DEBUG] Deleting an existing connection in the table matching the one from the current event:" << endl; 155 | cout << "\t" << (*old_connection_it).toNetFilterString() << endl; 156 | } 157 | 158 | this->connections.erase(old_connection_it); 159 | } 160 | 161 | switch (type) 162 | { 163 | case NFCT_T_NEW: 164 | case NFCT_T_UPDATE: 165 | { 166 | if (this->debugging) 167 | { 168 | if (type == NFCT_T_NEW) 169 | { 170 | if (exists) 171 | { 172 | cout << "[DEBUG] WARNING: Current connection was supposed to be new but it matched an existing one in our table (rebuilding=" 173 | << (this->is_rebuilding ? "true" : "false") 174 | << "):" << endl; 175 | cout << "\t" << connection.toNetFilterString() << endl; 176 | } 177 | } 178 | else 179 | { 180 | if (!exists) 181 | { 182 | cout << "[DEBUG] WARNING: Tried to update an existing connection in our table but a match was not found (rebuilding=" 183 | << (this->is_rebuilding ? "true" : "false") 184 | << "):" << endl; 185 | cout << "\t" << connection.toNetFilterString() << endl; 186 | } 187 | } 188 | } 189 | 190 | this->connections.push_back(connection); 191 | 192 | break; 193 | } 194 | 195 | case NFCT_T_DESTROY: 196 | { 197 | if (this->debugging) 198 | { 199 | if (!exists) 200 | { 201 | cout << "[DEBUG] WARNING: Tried to delete an existing connection in our table but a match was not found (rebuilding=" 202 | << (this->is_rebuilding ? "true" : "false") 203 | << "):" << endl; 204 | cout << "\t" << connection.toNetFilterString() << endl; 205 | } 206 | } 207 | break; 208 | } 209 | 210 | default: 211 | break; 212 | } 213 | } 214 | 215 | } // namespace conntrackex 216 | -------------------------------------------------------------------------------- /src/main.cc: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | #include "connection_table.h" 13 | 14 | using namespace std; 15 | using namespace conntrackex; 16 | 17 | 18 | // Allow Ctrl+C to break the main loop: 19 | static volatile int keep_running = 1; 20 | void sigint_handler(int) 21 | { 22 | cout << "Stopping conntrack_exporter" << endl; 23 | keep_running = 0; 24 | } 25 | 26 | // Source: https://stackoverflow.com/a/1493195 27 | template 28 | void tokenize(const std::string& str, 29 | ContainerT& tokens, 30 | const std::string& delimiters = " ", 31 | bool trimEmpty = false) 32 | { 33 | std::string::size_type pos, lastPos = 0, length = str.length(); 34 | 35 | using value_type = typename ContainerT::value_type; 36 | using size_type = typename ContainerT::size_type; 37 | 38 | while (lastPos < length + 1) 39 | { 40 | pos = str.find_first_of(delimiters, lastPos); 41 | if (pos == std::string::npos) 42 | pos = length; 43 | 44 | if (pos != lastPos || !trimEmpty) 45 | tokens.push_back(value_type(str.data()+lastPos, (size_type)pos-lastPos)); 46 | 47 | lastPos = pos + 1; 48 | } 49 | } 50 | 51 | int main(int argc, char** argv) 52 | { 53 | signal(SIGINT, sigint_handler); 54 | using namespace prometheus; 55 | 56 | // Parse arguments: 57 | argagg::parser argument_parser {{ 58 | 59 | { "bind_address", {"-b", "--bind-address"}, "The IP address on which to bind the metrics HTTP endpoint (default: 0.0.0.0)", 1 }, 60 | { "listen_port", {"-l", "--listen-port"}, "The port on which to expose the metrics HTTP endpoint (default: 9318)", 1 }, 61 | { "listen_path", {"-p", "--listen-path"}, "The path on which to expose the metrics HTTP endpoint (default: /metrics)", 1 }, 62 | { "ignore_hosts", {"-i", "--ignore-hosts"}, "Comma-separated list of hosts to ignore", 1 }, 63 | { "log_events", {"-e", "--log-events"}, "Enables logging of connection events", 0 }, 64 | { "log_events_format", {"-f", "--log-events-format"}, "Connection events log format (netfilter [default] or json)", 1 }, 65 | { "debug", {"-d", "--debug"}, "Enables logging of debug messages", 0 }, 66 | { "help", {"-h", "--help"}, "Print help and exit", 0 }, 67 | 68 | }}; 69 | ostringstream usage; 70 | usage << "Usage: " << argv[0] << " [options]" << endl; 71 | argagg::fmt_ostream help(cerr); 72 | argagg::parser_results args; 73 | try 74 | { 75 | args = argument_parser.parse(argc, argv); 76 | } 77 | catch (const std::exception& e) 78 | { 79 | help << "ERROR: " << e.what() << endl 80 | << endl 81 | << usage.str() << endl 82 | << argument_parser << endl; 83 | return EXIT_FAILURE; 84 | } 85 | if (args["help"]) 86 | { 87 | help << usage.str() << endl 88 | << argument_parser << endl; 89 | return EXIT_SUCCESS; 90 | } 91 | 92 | // Read arguments and set defaults when needed: 93 | const string bind_address = args["bind_address"] ? 94 | args["bind_address"].as() : 95 | "0.0.0.0"; 96 | const string guessed_local_endpoint = (bind_address == "0.0.0.0" || bind_address == "127.0.0.1") ? 97 | "localhost" : 98 | bind_address; 99 | const string listen_port = args["listen_port"] ? 100 | args["listen_port"].as() : 101 | "9318"; // see https://github.com/prometheus/prometheus/wiki/Default-port-allocations 102 | const string listen_path = args["listen_path"] ? 103 | args["listen_path"].as() : 104 | "/metrics"; 105 | 106 | try 107 | { 108 | cout << "conntrack_exporter v0.3.1" << endl; 109 | 110 | Exposer exposer{bind_address + ":" + listen_port}; 111 | cout << "Serving metrics at http://" + guessed_local_endpoint + ":" << listen_port << listen_path + " ..." << endl; 112 | 113 | ConnectionTable table; 114 | if (args["log_events"]) 115 | table.enableLogging(); 116 | if (args["log_events_format"]) 117 | table.setLoggingFormat(args["log_events_format"]); 118 | if (args["debug"]) 119 | table.enableDebugging(); 120 | if (args["ignore_hosts"]) 121 | { 122 | list ignored_hosts; 123 | tokenize(args["ignore_hosts"], ignored_hosts, ", \t", true); 124 | for (auto& host : ignored_hosts) 125 | { 126 | table.addIgnoredHost(host); 127 | 128 | if (args["debug"]) 129 | cout << "[DEBUG] Added to ignored host list: '" << host << "'" << endl; 130 | } 131 | } 132 | 133 | Connection::loadLocalIPAddresses(args["debug"]); 134 | 135 | table.attach(); 136 | while (keep_running) { 137 | 138 | // Build up a registry and metric families: 139 | auto registry = std::make_shared(); 140 | auto& opening_connections_family = BuildGauge() 141 | .Name("conntrack_opening_connections") 142 | .Help("How many connections to the remote host are currently opening?") 143 | .Register(*registry); 144 | auto& open_connections_family = BuildGauge() 145 | .Name("conntrack_open_connections") 146 | .Help("How many open connections are there to the remote host?") 147 | .Register(*registry); 148 | auto& closing_connections_family = BuildGauge() 149 | .Name("conntrack_closing_connections") 150 | .Help("How many connections to the remote host are currently closing?") 151 | .Register(*registry); 152 | auto& closed_connections_family = BuildGauge() 153 | .Name("conntrack_closed_connections") 154 | .Help("How many connections to the remote host have recently closed?") 155 | .Register(*registry); 156 | 157 | // Add guages for the individual connections: 158 | table.update(); 159 | for (auto& connection : table.getConnections()) 160 | { 161 | if (!connection.hasState()) 162 | continue; 163 | 164 | Gauge* pGauge; 165 | switch (connection.getState()) 166 | { 167 | case ConnectionState::OPENING: 168 | pGauge = &opening_connections_family.Add({{"host", connection.getRemoteHost()}}); 169 | break; 170 | case ConnectionState::OPEN: 171 | pGauge = &open_connections_family.Add({{"host", connection.getRemoteHost()}}); 172 | break; 173 | case ConnectionState::CLOSING: 174 | pGauge = &closing_connections_family.Add({{"host", connection.getRemoteHost()}}); 175 | break; 176 | case ConnectionState::CLOSED: 177 | pGauge = &closed_connections_family.Add({{"host", connection.getRemoteHost()}}); 178 | break; 179 | default: 180 | continue; 181 | } 182 | pGauge->Increment(); 183 | } 184 | exposer.RegisterCollectable(registry, listen_path); 185 | 186 | this_thread::sleep_for(chrono::seconds(1)); 187 | } 188 | } 189 | catch (const exception& e) 190 | { 191 | cout << "ERROR: " << e.what() << endl; 192 | exit(EXIT_FAILURE); 193 | } 194 | 195 | return EXIT_SUCCESS; 196 | } 197 | -------------------------------------------------------------------------------- /src/connection.cc: -------------------------------------------------------------------------------- 1 | #include "connection.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | 11 | namespace conntrackex { 12 | 13 | using namespace std; 14 | 15 | list Connection::local_ip_addresses; 16 | 17 | Connection::Connection(nf_conntrack* ct) 18 | { 19 | this->conntrack = nfct_clone(ct); 20 | } 21 | 22 | Connection::Connection(const Connection &other) 23 | { 24 | this->conntrack = nfct_clone(other.conntrack); 25 | } 26 | 27 | Connection::~Connection() 28 | { 29 | nfct_destroy(this->conntrack); 30 | this->conntrack = NULL; 31 | } 32 | 33 | // Source and destination that initiated the connection: 34 | string Connection::getOriginalSourceIP() const { return ip32ToString(nfct_get_attr_u32(this->conntrack, ATTR_ORIG_IPV4_SRC )); } 35 | uint16_t Connection::getOriginalSourcePort() const { return ntohs(nfct_get_attr_u16( this->conntrack, ATTR_ORIG_PORT_SRC )); } 36 | string Connection::getOriginalDestinationIP() const { return ip32ToString(nfct_get_attr_u32(this->conntrack, ATTR_ORIG_IPV4_DST )); } 37 | uint16_t Connection::getOriginalDestinationPort() const { return ntohs(nfct_get_attr_u16( this->conntrack, ATTR_ORIG_PORT_DST )); } 38 | 39 | // Source and destination of the expected (in case of [UNREPLIED]) or actual (in case of [ASSURED]) response: 40 | string Connection::getReplySourceIP() const { return ip32ToString(nfct_get_attr_u32(this->conntrack, ATTR_REPL_IPV4_SRC )); } 41 | uint16_t Connection::getReplySourcePort() const { return ntohs(nfct_get_attr_u16( this->conntrack, ATTR_REPL_PORT_SRC )); } 42 | string Connection::getReplyDestinationIP() const { return ip32ToString(nfct_get_attr_u32(this->conntrack, ATTR_REPL_IPV4_DST )); } 43 | uint16_t Connection::getReplyDestinationPort() const { return ntohs(nfct_get_attr_u16( this->conntrack, ATTR_REPL_PORT_DST )); } 44 | 45 | string Connection::getRemoteHost() const 46 | { 47 | if (isLocalIPAddress(this->getOriginalSourceIP())) 48 | return this->getOriginalDestinationHost(); 49 | else if (isLocalIPAddress(this->getOriginalDestinationIP())) 50 | return this->getOriginalSourceHost(); 51 | else if (isLocalIPAddress(this->getReplySourceIP())) 52 | return this->getReplyDestinationHost(); 53 | else 54 | { 55 | // if (!isLocalIPAddress(this->getReplyDestinationIP())) 56 | // cerr << "[WARNING] Couldn't identify a local IP address in a connection." << endl; 57 | 58 | return this->getReplySourceHost(); 59 | } 60 | } 61 | 62 | bool Connection::hasState() const 63 | { 64 | return (nfct_attr_is_set(this->conntrack, ATTR_TCP_STATE) != -1 && 65 | nfct_get_attr_u8(this->conntrack, ATTR_TCP_STATE) != TCP_CONNTRACK_NONE); 66 | } 67 | 68 | ConnectionState Connection::getState() const 69 | { 70 | // Calling this method on a connection with no state is a bug: 71 | if (!this->hasState()) 72 | throw logic_error("Connection state not available."); 73 | 74 | auto tcp_state = nfct_get_attr_u8(this->conntrack, ATTR_TCP_STATE); 75 | 76 | // We don't expect to see MAX or IGNORE. 77 | assert(tcp_state != TCP_CONNTRACK_MAX); 78 | assert(tcp_state != TCP_CONNTRACK_IGNORE); 79 | 80 | switch (tcp_state) 81 | { 82 | case TCP_CONNTRACK_SYN_SENT: 83 | case TCP_CONNTRACK_SYN_SENT2: 84 | case TCP_CONNTRACK_SYN_RECV: 85 | return ConnectionState::OPENING; 86 | case TCP_CONNTRACK_ESTABLISHED: 87 | return ConnectionState::OPEN; 88 | case TCP_CONNTRACK_FIN_WAIT: 89 | case TCP_CONNTRACK_CLOSE_WAIT: 90 | case TCP_CONNTRACK_LAST_ACK: 91 | case TCP_CONNTRACK_TIME_WAIT: 92 | return ConnectionState::CLOSING; 93 | case TCP_CONNTRACK_CLOSE: 94 | return ConnectionState::CLOSED; 95 | } 96 | 97 | assert(false); 98 | return ConnectionState::CLOSED; 99 | } 100 | 101 | string Connection::toString() const 102 | { 103 | stringstream output; 104 | output << "{"; 105 | 106 | if (this->hasEventType()) 107 | output << "\"event_type\":\"" << this->getEventTypeString() << "\","; 108 | 109 | output 110 | << "\"original_source_host\":\"" << this->getOriginalSourceHost() << "\"," 111 | << "\"original_destination_host\":\"" << this->getOriginalDestinationHost() << "\"," 112 | << "\"reply_source_host\":\"" << this->getReplySourceHost() << "\"," 113 | << "\"reply_destination_host\":\"" << this->getReplyDestinationHost() << "\"," 114 | << "\"remote_host\":\"" << this->getRemoteHost() << "\"," 115 | << "\"state\":\"" << (this->hasState() ? this->getStateString() : "None") << "\"" 116 | << "}"; 117 | return output.str(); 118 | } 119 | 120 | string Connection::toNetFilterString() const 121 | { 122 | stringstream output; 123 | 124 | if (this->hasEventType()) 125 | output << "event=" << left << std::setw(10) << this->getEventTypeString() << " "; 126 | 127 | char buffer[1024]; 128 | nfct_snprintf(buffer, sizeof(buffer), this->conntrack, NFCT_T_ALL, NFCT_O_DEFAULT, NFCT_OF_TIME | NFCT_OF_TIMESTAMP | NFCT_OF_SHOW_LAYER3); 129 | output << buffer; 130 | 131 | return output.str(); 132 | } 133 | 134 | bool Connection::operator==(const Connection& other) const 135 | { 136 | return (nfct_cmp(this->conntrack, other.conntrack, NFCT_CMP_ORIG | NFCT_CMP_REPL) == 1); 137 | } 138 | 139 | string Connection::ip32ToString(uint32_t ip32) 140 | { 141 | char output[INET_ADDRSTRLEN]; 142 | 143 | return (inet_ntop(AF_INET, (void*)&ip32, output, INET_ADDRSTRLEN) != NULL) ? 144 | string((char*)&output) : 145 | string(""); 146 | } 147 | 148 | string Connection::stateToString(const ConnectionState state) 149 | { 150 | switch (state) 151 | { 152 | case ConnectionState::OPENING: return "Opening"; 153 | case ConnectionState::OPEN: return "Open"; 154 | case ConnectionState::CLOSING: return "Closing"; 155 | case ConnectionState::CLOSED: return "Closed"; 156 | } 157 | 158 | return ""; 159 | } 160 | 161 | inline const string Connection::getEventTypeString() const 162 | { 163 | return 164 | (this->event_type == NFCT_T_NEW) ? "new" : 165 | (this->event_type == NFCT_T_UPDATE) ? "update" : 166 | (this->event_type == NFCT_T_DESTROY) ? "destroy" : 167 | ""; 168 | } 169 | 170 | void Connection::loadLocalIPAddresses(bool log_debug_messages) 171 | { 172 | // This method inspired by GetNetworkInterfaceInfos() in 173 | // https://public.msli.com/lcs/muscle/muscle/util/NetworkUtilityFunctions.cpp 174 | 175 | // Singleton gatekeeper: 176 | static bool initalized = false; 177 | if (initalized) 178 | return; 179 | initalized = true; 180 | 181 | struct ifaddrs* ifap; 182 | if (getifaddrs(&ifap) != 0) 183 | { 184 | cerr << "[WARNING] Can't get local network interface addresses." << endl; 185 | return; 186 | } 187 | 188 | auto current_ifap = ifap; 189 | while(current_ifap) 190 | { 191 | //const string interface_name = current_ifap->ifa_name; 192 | if (current_ifap->ifa_addr && 193 | current_ifap->ifa_addr->sa_family == AF_INET) 194 | { 195 | auto ip_address = ((struct sockaddr_in*)current_ifap->ifa_addr)->sin_addr.s_addr; 196 | auto ip_address_str = ip32ToString(ip_address); 197 | Connection::local_ip_addresses.push_back(ip_address_str); 198 | 199 | if (log_debug_messages) 200 | cout << "[DEBUG] Found local IP: '" << ip_address_str << "'" << endl; 201 | } 202 | 203 | current_ifap = current_ifap->ifa_next; 204 | } 205 | 206 | freeifaddrs(ifap); 207 | } 208 | 209 | bool Connection::isLocalIPAddress(const string& ip_address) 210 | { 211 | loadLocalIPAddresses(); 212 | return (find(Connection::local_ip_addresses.begin(), 213 | Connection::local_ip_addresses.end(), 214 | ip_address) != Connection::local_ip_addresses.end()); 215 | } 216 | 217 | } // namespace conntrackex 218 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # **conntrack_exporter** 2 | 3 | *Prometheus exporter for tracking network connections* 4 | 5 | ![Grafana screenshot](grafana.png) 6 | 7 | ## Uses 8 | 9 | * **Operations**: monitor when a critical link between your microservices is broken. 10 | * **Security**: monitor which remote hosts your server is talking to. 11 | * **Debugging**: correlate intermittently misbehaving code with strange connection patterns. 12 | * **Audit**: store JSON logs of connection events for future analysis. 13 | 14 | 15 | ## Features 16 | 17 | conntrack_exporter exposes Prometheus metrics showing the state and remote endpoint for each connection on the server. For example: 18 | 19 | ``` 20 | # HELP conntrack_opening_connections How many connections to the remote host are currently opening? 21 | # TYPE conntrack_opening_connections gauge 22 | conntrack_opening_connections{host="10.0.1.5:3306"} 2 23 | conntrack_opening_connections{host="10.0.1.12:8080"} 0 24 | 25 | # HELP conntrack_open_connections How many open connections are there to the remote host? 26 | # TYPE conntrack_open_connections gauge 27 | conntrack_open_connections{host="10.0.1.5:3306"} 49 28 | conntrack_open_connections{host="10.0.1.12:8080"} 19 29 | 30 | # HELP conntrack_closing_connections How many connections to the remote host are currently closing? 31 | # TYPE conntrack_closing_connections gauge 32 | conntrack_closing_connections{host="10.0.1.5:3306"} 0 33 | conntrack_closing_connections{host="10.0.1.12:8080"} 1 34 | 35 | # HELP conntrack_closed_connections How many connections to the remote host have recently closed? 36 | # TYPE conntrack_closed_connections gauge 37 | conntrack_closed_connections{host="10.0.1.5:3306"} 3 38 | conntrack_closed_connections{host="10.0.1.12:8080"} 0 39 | ``` 40 | 41 | Optionally, it can also emit logs of connection events. Ship these logs to your favourite log processors and alerting systems or archive them for future audit capabilities. 42 | 43 | 44 | ## Quick Start 45 | 46 | ``` 47 | docker run -d --cap-add=NET_ADMIN --net=host --name=conntrack_exporter hiveco/conntrack_exporter 48 | ``` 49 | 50 | Then open http://localhost:9318/metrics in your browser to view the Prometheus metrics. 51 | 52 | To change the listen port: 53 | 54 | ``` 55 | docker run -d --cap-add=NET_ADMIN --net=host --name=conntrack_exporter hiveco/conntrack_exporter --listen-port=9101 56 | ``` 57 | 58 | Run with `--help` to see all available options. 59 | 60 | ## Logging 61 | 62 | conntrack_exporter can emit logs of all connection events it processes. Example: 63 | 64 | ``` 65 | $ docker run -it --rm --cap-add=NET_ADMIN --net=host hiveco/conntrack_exporter --log-events --log-events-format=json 66 | ... 67 | {"event_type":"new","original_source_host":"10.0.1.65:40806","original_destination_host":"151.101.2.49:443","reply_source_host":"151.101.2.49:443","reply_destination_host":"10.0.1.65:40806","remote_host":"151.101.2.49:443","state":"Open"} 68 | {"event_type":"new","original_source_host":"10.0.1.65:34900","original_destination_host":"162.247.242.20:443","reply_source_host":"162.247.242.20:443","reply_destination_host":"10.0.1.65:34900","remote_host":"162.247.242.20:443","state":"Opening"} 69 | ``` 70 | 71 | In the typical case, the `remote_host` and `state` keys would be the most interesting. `event_type` and the keys prefixed `original_` and `reply_` expose slightly lower level information obtained from libnetfilter_conntrack. 72 | 73 | The `--log-events-format` argument currently supports two logging formats: `json` or `netfilter` (default) for the familiar and human-friendly [conntrack tools](http://conntrack-tools.netfilter.org/) format. 74 | 75 | ## Building 76 | 77 | Prerequisites: 78 | 79 | * [Bazel](https://www.bazel.build/) (tested with v6.4.0) 80 | * libnetfilter-conntrack-dev (Ubuntu/Debian: `apt-get install libnetfilter-conntrack-dev`) 81 | 82 | conntrack_exporter builds as a mostly-static binary, only requiring that the `libnetfilter_conntrack` library is available on the system. To build the binary, run `make`. To build the `hiveco/conntrack_exporter` Docker image, run `make build_docker`. 83 | 84 | NOTE: Building is only tested on Ubuntu 22.04. 85 | 86 | 87 | ## Connection States 88 | 89 | There are four possible states a TCP connection can be in: opening, open, closing, and closed. These map to traditional [TCP states](https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.1.0/com.ibm.zos.v2r1.halu101/constatus.htm) as follows: 90 | 91 | |TCP State|Reported As| 92 | |-----|-----| 93 | |SYN_SENT|Opening| 94 | |SYN_RECV|Opening| 95 | |ESTABLISHED|Open| 96 | |FIN_WAIT|Closing| 97 | |CLOSE_WAIT|Closing| 98 | |LAST_ACK|Closing| 99 | |TIME_WAIT|Closing| 100 | |CLOSE|Closed| 101 | 102 | The TCP states are generalized as above because they tend to be a useful abstraction for typical users, and because they help minimize overhead. 103 | 104 | 105 | ## FAQs 106 | 107 | ### Doesn't Prometheus already monitor the system's connections natively? 108 | 109 | Not exactly. Prometheus's [node_exporter](https://github.com/prometheus/node_exporter/) has the disabled-by-default `tcpstat` module, which exposes the number of connections in each TCP state. It does not expose which remote hosts are being connected to and which states those connections are in. 110 | 111 | Why not? One difficulty is that `tcpstat` parses `/proc/net/tcp` to obtain its metrics, which can become slow on a busy server. In addition, there would be significant overhead for the Prometheus server to scrape and store the large amount of constantly-changing label:value pairs of metrics that such a busy server would expose. As stated in the [docs](https://github.com/prometheus/node_exporter/commit/e2163db0f7a8f16ba9f505d9ca72bc2c68696e7d#diff-04c6e90faac2675aa89e2176d2eec7d8R54) accompanying tcpstat, "the current version has potential performance issues in high load situations". So the Prometheus authors decided to expose only totals for each TCP state, which is quite a reasonable choice for the typical user. 112 | 113 | conntrack_exporter exists to put that choice in the hands of the user. It is written in C++ (`node_exporter` is written in Golang) and instead of parsing `/proc/net/tcp`, it uses [libnetfilter_conntrack](https://www.netfilter.org/projects/libnetfilter_conntrack/) for direct access to the Linux kernel's connection table. This should make it reasonably fast even on busy servers, and allows more visibility into what's behind the summarized totals exposed by `tcpstat`. 114 | 115 | ### Should I run this on a server that listens for external Internet connections? 116 | 117 | Probably not, since a large number of unique connecting clients will create many metric labels and your Prometheus instance may be overwhelmed. conntrack_exporter is best used with internal servers (like application servers behind a load balancer, databases, caches, queues, etc), since the total number of remote endpoints these connect to tends to be small and fixed (i.e. usually just the other internal services behind your firewall). 118 | 119 | ### I know some open connections were closed, why is `conntrack_closed_connections` not reporting them? 120 | 121 | conntrack_exporter just exposes the system's connection table in a format Prometheus can scrape, and it's likely the closed connections are being dropped from the system table very quickly. It could be that this guage goes up for some short period and then goes back down again before your Prometheus server can scrape it. 122 | 123 | Either increase the scrape frequency so Prometheus is more likely to notice the change, or increase how long the system "remembers" closed connections, which is controlled by `nf_conntrack_tcp_timeout_close` (this value is in seconds). 124 | 125 | Check the current setting: 126 | ``` 127 | sysctl net.netfilter.nf_conntrack_tcp_timeout_close 128 | ``` 129 | 130 | Update it temporarily (lasts until next reboot): 131 | ``` 132 | sysctl -w net.netfilter.nf_conntrack_tcp_timeout_close=60 133 | ``` 134 | 135 | Update it permanently: 136 | ``` 137 | echo "net.netfilter.nf_conntrack_tcp_timeout_close=60" >> /etc/sysctl.conf 138 | sysctl -p 139 | ``` 140 | 141 | **WARNING:** Raising this setting too high is not recommended, especially on high-traffic servers, because it may overflow your system's connection table due to all the extra closed connections it has to keep track of. 142 | 143 | Similar issues with other connection states (besides `closed`) might be resolved by updating the other `net.netfilter.nf_conntrack_tcp_timeout_*` settings as appropriate. Run `sysctl -a | grep conntrack | grep timeout` to see all available settings. 144 | 145 | ### It's great, but I wish it... 146 | 147 | Please open a [new issue](https://github.com/hiveco/conntrack_exporter/issues/new). 148 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | --------------------------------------------------------------------------------